Java high-frequency basic interview questions——(6)

王林
Release: 2020-09-07 18:00:09
forward
2358 people have browsed it

Java high-frequency basic interview questions——(6)

1. Talk about your understanding of Struts

(Recommended more related interview questions: java interview questions and answers)

1. Struts is a Web layer framework designed according to the MVC pattern. In fact, it is a Servlet. This Servlet is called ActionServlet, or a subclass of ActionServlet. We can hand over all requests that meet certain characteristics in the web.xml file to this Servlet for processing. This Servlet then refers to a configuration file to assign each request to different actions for processing.

(Struts can have multiple configuration files, and each configuration file can be configured according to the module, which can prevent excessive expansion of the configuration file)

2. ActionServlet hands the request to action for processing Previously, the request parameters were encapsulated into a formbean object (that is, a java class, each attribute in this class corresponds to a request parameter).

3. It should be noted that ActionServlet passes the formbean object to the action Before the execute method, the formbean's validate method may be called for verification. Only after the verification passes, the formbean object will be passed to the action's execute method. Otherwise, it will return an error page. This error page is specified by the input attribute.

4. After the action is executed, the displayed result view should be returned. This result view is represented by an ActionForward object. The actionForward object is associated to a jsp page through the configuration in the struts-config.xml configuration file. , because the program uses the logical name set for the jsp page in the struts-config.xml configuration file, so that the action program code and the returned jsp page name can be decoupled.

(Above, you can also talk about your own opinions based on your own experience of using it)

2. Talk about your understanding of Hibernate.

1. The internal operation process of object-oriented software can be understood as the process of constantly creating various new objects, establishing relationships between objects, and calling object methods to change the status of each object and the object's demise. , No matter what the process and operation of the program are, it is essentially to get a result. The difference between the running results of the program at the previous moment and the next moment is reflected in the change in the state of the object in the memory.

2. In order to maintain the running status of the program when it is shut down and the memory space is insufficient, it is necessary to save the object state in the memory to the persistence device and restore the object state from the persistence device. Usually They are all saved to a relational database to save a large amount of object information. From the perspective of the running function of a Java program, the function of saving object status should be a very inconspicuous subsidiary function compared to other functions of system operation. Java uses jdbc to implement this function, but this inconspicuous function requires writing There is a lot of code, and the only thing it does is save objects and restore objects, and those large amounts of jdbc code have no technical content. They are basically written using a set of routine standard code templates, which is a kind of hard work and repetitive Work.

3. Saving the objects and recovery objects generated when the Java program is running through the database actually realizes the mapping relationship between Java objects and relational database records, which is called ORM (ie Object RelationMapping). People can encapsulate JDBC Code is used to implement this function, and the encapsulated product is called an ORM framework. Hibernate is one of the popular ORM frameworks. Using the Hibernate framework, you don't need to write JDBC code. You can save an object to a relational database by just calling a save method. You can load an object from the database by just calling a get method.

4. The basic process of using Hibernate is: configure the Configuration object, generate the SessionFactory, create the session object, start the transaction, complete the CRUD operation, submit the transaction, and close the session.

5. When using Hibernate, you must first configure the hibernate.cfg.xml file, which configures the database connection information and dialects, etc., and also configures the corresponding hbm.xml file for each entity, hibernate.cfg.xml Each hbm.xml file needs to be registered in the file.

6. When applying Hibernate, it is important to understand the caching principle of Session, cascading, lazy loading and hql query.

(Above, you can also talk about your feelings about hibernate based on your own cumbersome experience in using JDBC)

Third, talk about your understanding of Spring.

1. Spring is a factory class that implements the factory pattern (it is necessary to explain clearly what the factory pattern is here). This class is called BeanFactory (actually an interface). In the program, it is usually BeanFactory subclass ApplicationContext. Spring is equivalent to a large factory class. In its configuration file, the class name used to create instance objects and the properties of the instance object are configured through the element.

2. Spring provides good support for IOC. IOC is a programming idea and an architectural art. This idea can be used to achieve good decoupling between modules. IOC is also called DI. (Depency Injection).

3. Spring provides a good encapsulation of AOP technology. AOP is called aspect-oriented programming, which means that there are many methods of unrelated classes in the system. Code for certain system functions must be added to these many methods, such as , add logging, add permission judgment, and add exception handling. This application is called AOP.

AOP function is implemented using proxy technology. The client program no longer calls the target, but calls the proxy class. The proxy class and the target class have the same method statement externally. There are two ways to achieve the same method statement. , one is to implement the same interface, and the other is as a subclass of the target.

In the JDK, the Proxy class is used to generate a dynamic proxy to generate an implementation class for an interface. If you want to generate a subclass for a certain class, you can use CGLI B. Add the system function and the corresponding method of calling the target class to the method of the generated proxy class. The proxy of the system function is provided as an Advice object. Obviously, to create a proxy object, at least the target class and the Advice class are required. Spring provides this support, and you only need to configure these two elements in the spring configuration file to implement the proxy and aop functions.

(For the above, you can also talk about your own opinions based on your own experience of using it)

4. Talk about the advantages and disadvantages of Struts

Advantages:

1. Implement the MVC model with a clear structure, allowing developers to only focus on the implementation of business logic.

2. There are a wealth of tags available. The Struts tag library (Taglib) can greatly improve development if it can be used flexibly. Efficiency

3. Page navigation makes the context of the system clearer. Through a configuration file, you can grasp the connection between various parts of the entire system, which is of great benefit for later maintenance. This advantage becomes even more obvious when another group of developers takes over the project.

4. Provide Exception handling mechanism.

5. Database connection pool management

6. Support I18N

Disadvantages:

1 , when switching to the display layer, you need to configure forward. If there are ten jsps in the display layer, you need to configure struts ten times, and this does not include sometimes directory and file changes. You need to modify the forward again. Note that after each modification of the configuration, The entire project is required to be redeployed, and a server like tomcate must also be restarted.

2. The Action of Struts must be in thread-safe mode, which only allows one instance to handle all requests. Therefore, all resources used by actions must be uniformly synchronized, which causes thread safety issues.

3. Testing is inconvenient. Each Action of Struts is coupled with the Web layer, so its testing depends on the Web container, and unit testing is also difficult to implement. However, there is a Junit extension tool Struts TestCase that can implement its unit testing.

4. Type conversion. Struts' FormBean treats all data as String type, and it can use the tool Commons-Beanutils for type conversion. But its conversion is all at the Class level, and the conversion type is not configurable. It is also very difficult to return error messages during type conversion to the user.

5. Too much dependence on Servlet. Struts must rely on ServletRequest and ServletResponse when processing Action, so it cannot get rid of the Servlet container.

6. In terms of front-end expression language, Struts integrates JSTL, so it mainly uses JSTL expression language to obtain data. However, JSTL's expression language is very weak in handling Collection and index properties.

7. It is difficult to control the execution of Action. When Struts creates an Action, it will be very difficult to control its execution order. You may even have to re-write the Servlet to realize your functional requirements.

8. Processing before and after Action execution. When Struts processes Action, it is based on class hierarchies. It is difficult to operate before and after action processing.

9. Insufficient support for events. In Struts, a form actually corresponds to an Action class (or DispatchAction). In other words: In Struts, a form actually corresponds to only one event. Struts This event method is called application event. Compared with component event, application event is a coarse-grained event

(Video tutorial recommendation: java course)

5. What is the difference between iBatis and Hibernate?

The same points: shielding the underlying access details of jdbc api, so that we can access data without having to deal with jdbc api.

The jdbc api programming process is fixed, and it also mixes sql statements with java code. It is often necessary to piece together sql statements, and the details are very cumbersome.

Benefits of ibatis: shields the underlying access details of jdbc api; separates sql statements from java code; provides the function of automatically encapsulating result sets called entity objects and collections of objects. queryForList returns a collection of objects. Use queryForObject to return a single object; provides parameters that automatically pass the properties of the entity object to the SQL statement.

Hibernate is a fully automatic orm mapping tool, which can automatically generate sql statements. ibatis requires us to write sql statements in the xml configuration file ourselves. Hibernate is much more responsible and powerful than ibatis. Because hibernate automatically generates sql statements, we cannot control the statements, and we cannot write specific and efficient sql. For some less complex SQL queries, hibernate can help us complete it. However, for particularly complex queries, hibernate is difficult to adapt to. At this time, using ibatis is a good choice, because ibatis still requires us to write the sql statements ourselves. .

6. Perform multi-table query in hibernate and select several fields from each table. That is to say, the query result set does not have an entity class corresponding to it. How to solve this problem?

Solution 1: Get the data according to the Object[] data, and then form the bean yourself

Solution 2: Write a constructor for the bean of each table, for example, table 1 needs to find field1, field2 has two fields, then there is a constructor called Bean(type1filed1,type2 field2), and then this bean can be generated directly in hql.

7. Introduce Hibernate’s second-level cache

Answer according to the following ideas:

(1) First, explain clearly what cache is

(2) Besides, hibernate's Session is the first-level cache. That is, if there is a first-level cache, why do we need a second-level cache?

(3) Finally, let's talk about how to configure Hibernate's second-level cache.

1. Caching is to store objects previously queried and used from the database in memory (in a data structure). This data structure is usually or similar to HashMap, when an object is to be used in the future. , first query whether there is this object in the cache, if so, use the object in the cache, if not, query the database, and save the queried object in the cache for next time use.

2. Hibernate's Session is a kind of cache. We usually call it Hibernate's first-level cache. When you want to use session to query an object from the database, Session also first checks whether it exists internally. If this object exists, it will be returned directly. If it does not exist, it will access the database and save the query results internally.

Since Session represents a session process, and a Session is associated with a database connection, it is best not to keep the Session open for a long time. It is usually only used in one transaction and should be closed at the end of the transaction. And Session is thread-unsafe and prone to problems when shared by multiple threads. Usually only the cache in the global sense is the real cache application and has greater cache value. Therefore, the cache function of Hibernate's Session level cache is not obvious and the application value is not great. Hibernate's second-level cache is to configure a global cache for Hibernate so that multiple threads and multiple transactions can share this cache. What we hope is that once one person has used it, others can also use it. Session does not have this effect.

3. The second-level cache is a software component independent of Hibernate and is a third-party product. Multiple manufacturers and organizations provide cache products, such as EHCache and OSCache, etc. To use the second-level cache in Hibernate, you must first configure which manufacturer's cache product to use in the hibernate.cfg.xml configuration file. Then you need to configure the cache product's own configuration file. Finally, you need to configure which entity objects in Hibernate should be included. to the management of the second level cache. After understanding the principle of second-level cache and having this idea, it is easy to configure Hibernate's second-level cache.

Extended knowledge: A SessionFactory can be associated with a second-level cache, that is, a second-level cache can only be responsible for caching data in one database. When using Hibernate's second-level cache, be careful not to have other applications or SessionFactory to change the data in the current database so that the cached data will be inconsistent with the actual data in the database.

8. What is JDO?

JDO is a new specification for Java object persistence. It is the abbreviation of java data object and is also a tool used to access certain data warehouses. Standardized API for objects. JDO provides transparent object storage, so for developers, no additional code (such as the use of JDBC API) is required to store data objects. These tedious routine tasks have been transferred to JDO product providers, freeing developers to focus their time and energy on business logic. In addition, JDO is flexible because it can run on any data underlying.

Comparison: JDBC is only for relational databases (RDBMS). JDO is more general and provides underlying storage functions for any data, such as relational databases, files, XML and object databases (ODBMS), etc., making applications More portable.

9. What is the difference between Hibernate's one-to-many and many-to-many bidirectional associations? ?

The basic principles of one-to-many association mapping and many-to-one association mapping are the same, that is, adding a foreign key on the many end to point to the foreign key on the one end, and the main difference is that the maintenance end is different .

The difference lies in the relationships they maintain:

One-to-many association mapping refers to loading the data of many ends while loading the data of one end. Many-to-one association mapping refers to loading the data of many ends at the same time. Load the data of the multiple ends and load the data of the one end at the same time.

10. How does Hibernate delay loading?

1. Hibernate2 lazy loading implementation: a) Entity object b) Collection (Collection)

2. Hibernate3 provides the lazy loading function of attributes When Hibernate queries the data, the data does not exist in the memory. When the program actually operates on the data, the object exists in the memory, thus achieving lazy loading. It saves the memory overhead of the server, thereby improving the server's performance. performance.

Recommended related tutorials: java introductory tutorial

The above is the detailed content of Java high-frequency basic interview questions——(6). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template