Home > Java > JavaInterview questions > Java framework interview questions (1)

Java framework interview questions (1)

(*-*)浩
Release: 2019-12-07 15:07:12
Original
1792 people have browsed it

Java framework interview questions (1)

Talk about your understanding of Struts.

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. (Recommended study: java interview questions)

We can hand over all requests that meet certain characteristics in the web.xml file to this Servlet for processing, and this Servlet will refer to a configuration file. Each request is assigned to a different action 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 is 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, this can achieve the decoupling of the action program code and the returned jsp page name.

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

Talk about your understanding of Hibernate.

1. The internal operation process of object-oriented software can be understood as constantly creating various new objects, establishing relationships between objects, and calling object methods to change the status of each object and the object. The process of death, regardless of the process and operation of the program, is essentially to obtain 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 shutting down or insufficient memory space, 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 Java programs, 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. This is not The eye-catching function requires writing a lot of code, and the only thing it does is to 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. It's 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 configure 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)

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)

Talk about the advantages and disadvantages of Struts

Advantages:

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

2. There are a wealth of tags that can be used, and the Struts tag library (Taglib), if used flexibly, can greatly improve development 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:

First, when going 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, Every time the configuration is modified, the entire project is required to be redeployed, and a server like tomcate must be restarted.

Second, the Action of Struts must be thread-safe, which only allows one instance to handle all ask. Therefore, all resources used by actions must be uniformly synchronized, which causes thread safety issues.

Third, 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.

Four, 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.

Fifth, the dependence on Servlet is too strong. Struts must rely on ServletRequest and ServletResponse when processing Action, so it cannot get rid of the Servlet container.

Six, 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.

Seven, 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.

Eight, processing before and after action execution. Struts uses class-based hierarchies when processing action, and 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.

What is the difference between iBatis and Hibernate?

Same points: Shield 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.

The benefits of ibatis: shield the underlying access details of the jdbc api; separate the sql statement from the java code; provide the function of automatically encapsulating the result set called a collection of entity objects and 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. It 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 is still controlled by ourselves. Write sql statements.

When performing multi-table query in hibernate, how to take several fields from each table? That is to say, the query result set does not have a corresponding entity class. How to solve this problem?

Solution one: take out the data according to the Object[] data, and then form the bean yourself

Solution two: write a constructor for the bean of each table, for example, table one Find out the two fields field1 and field2, then there is a constructor called Bean(type1 filed1, type2 field2), and then you can directly generate this bean in hql.

Introduce Hibernate's second-level cache

Answer according to the following ideas:

(1) First, make it clear what It's cache

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

(3) Finally, let's talk about how to configure Hibernate. Level 2 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 there is, 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.

We hope 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.

What is JDO?

JDO is the new specification for Java object persistence. It is the abbreviation of java data object and is also a data warehouse used to access some kind of data. Standardized API for objects in . 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. Stronger.

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 at the many end to point to the foreign key at the one end. The main difference is that The maintenance side is different.

The difference lies in the relationships they maintain:

One-to-many association mapping refers to loading data from one end while loading multiple pairs of data from the many end. An associative mapping means loading the data of one end while loading the data of one end.

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 the data, the object exists in the memory, thus achieving lazy loading. , it saves the memory overhead of the server, thereby improving the performance of the server.

The above is the detailed content of Java framework interview questions (1). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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