This article tells the Spring,hibernate,struts Classic interview pen question and its reference answer. Share to everyone for your reference, specific as follows:
1.Hibernate working principle and why to use?
Principle:
1. Read and resolve configuration files
2. Read and parse mapping information, create Sessionfactory
3. Open Sesssion
4. Create Transaction Transation
5. Persistent operation
6. Submission of services
7. Close session
8. Close Sesstionfactory
Why to use:
①. The code of JDBC Access database is encapsulated, which simplifies the cumbersome repetitive code of the data access layer greatly.
Ii. Hibernate is a mainstream persistence framework based on JDBC and is an excellent ORM implementation. He simplifies the coding of the DAO layer to a large extent
③. Hibernate uses the Java reflection mechanism rather than the bytecode enhancement program to achieve transparency.
④. Hibernate's performance is very good because it is a lightweight framework. The flexibility of mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.
2. How does the hibernate delay loading?
①. Hibernate2 deferred load implementation: a) collection of entity object B (Collection)
Ii. Hibernate3 provides deferred loading of properties
When the hibernate is querying the data, the data does not exist in memory, when the program is actually operating on the data, the object exists and in memory, it realizes the delay loading, saves the memory cost of the server, and improves the performance of the server.
3. How does the relationship between classes be implemented in hibernate? (eg: one-to-many, many-to-many Relationships)
The relationship between class and class is mainly embodied in the relationship between table and table, they operate the objects in the city, we program all the tables and classes are mapped together, they pass the configuration file in the Many-to-one, One-to-many, Many-to-many,
4. Next Hibernate's caching mechanism
①. Internal cache exists in Hibernate, also known as the first-level cache, which belongs to the Application object cache
Ii. Second-level caching:
A) application and caching
b) Distributed caching
Conditions: Data will not be modified by the third party, the data size in the acceptable range, the data update frequency is low, the same data is frequently used by the system, non-critical data
(c) Implementation of third party caching
5. How to query Hibernate
SQL, Criteria,object comptosition
HQL:
1. Property Query
2, parameter query, named parameter query
3. Related Query
4, paging query
5. Statistical function
6. How to optimize hibernate?
①. Use bi-directional one-to-many associations, not using one-way one-to-many
②. Flexible use of one-way One-to-many Association
③. Don't be one-on-one, replace with multiple pairs.
④. Configure object caching, do not use collection caching
⑤. One-to-many collections Use the bag, and Many-to-many collections use Set
⑥. Inheriting classes use explicit polymorphism
⑦. Fewer table fields, less fear, and two cache-backed
7. How does struts work? Why do you use struts?
Working mechanism:
The work flow of struts:
Initialized Actionservlet,actionservlet is loaded when the Web application is started
Struts-config.xml files to read configuration information and store them in various configuration objects
When Actionservlet receives a client request, the following process is performed.
-(1) Retrieving the actionmapping instance that matches the user request and, if not, returning the invalid request path information;
-(2) If the Actionform instance does not exist, create a Actionform object to save the form data submitted by the customer to the Actionform object;
-(3) Determine if form validation is required based on configuration information. If validation is required, call the Actionform validate () method;
-(4) If the Actionform Validate () method returns null or returns a Actuiberrors object that does not contain actionmessage, the form validation succeeds;
-(5) Actionservlet determines which action is forwarded to the request according to the mapping information contained in the actionmapping, if the corresponding action instance does not exist, first creates the instance, and then invokes the action execute () method;
-(6) The Execute () method of the action returns a Actionforward object that actionservlet the client request to the JSP component that the Actionforward object points to;
-(7) Actionforward object to the JSP component to generate dynamic Web pages, return to the customer;
Why to use:
The emergence of JSP, Servlet and JavaBean technology provides us with the possibility to build a powerful enterprise application system. But the systems built with these technologies are Fanluan, so on top of that, we need a rule, a rule that organizes these technologies, and that's the framework in which struts emerges.
The application based on struts is composed of 3 kinds of components: Controller component, model component, view component
8. How is the validate framework of struts validated?
Configure specific error prompts in the struts configuration file, and then call the Validate () method in Formbean.
9. The design pattern of struts
MVC pattern: When a Web application starts, it loads and initializes the Actionservler. When a user submits a form, a configured Actionform object is created and filled in with the corresponding data in the form. Actionservler determines whether form validation is required based on the configured settings of the Struts-config.xml file, and if you need to call Actionform Validate (), select which action to send the request to, if the action does not exist , Actionservlet creates this object first, and then invokes the Execute () method of the action. Execute () obtains the data from the Actionform object, completes the business logic, returns a Actionforward object, and actionservlet the client request to the JSP component specified by the Actionforward object. The Actionforward object specifies that the JSP generates a dynamic Web page that is returned to the customer.
10. Spring working mechanism and why should it be used?
①.spring MVC invites all requests to be submitted to Dispatcherservlet, which will entrust the other modules of the application system with the responsibility of actually handling the request.
②.dispatcherservlet query one or more handlermapping to find the controller that handles the request.
③.dispatcherservlet request Submit to target Controller
④.controller after the business logic is processed, a modelandview is returned
⑤.dispathcher query One or more viewresolver view parsers to find the view object specified by the Modelandview object
⑥. The View object is responsible for rendering return to the client.
Why use:
{AOP allows developers to create non behavioral concerns, called crosscutting concerns, and insert them into application code. With AOP, public services (such as logs, persistence, transactions, and so on) can be decomposed into aspects and applied to domain objects without increasing the complexity of the object model of the domain object.
IOC allows the creation of an application environment in which objects can be constructed, and then passes their collaboration objects to those objects. As the word inversion shows, the IOC is like the reverse JNDI. Not using a bunch of abstract factories, service locators, single elements (singleton), and direct constructs (straight construction), each object is constructed with its collaboration object. Therefore, the container manages the collaboration object (collaborator).
Spring, even an AOP framework, is an IOC container. The best part of Spring is that it helps you replace objects. With Spring, you just add dependencies (collaboration objects) with the JavaBean attribute and the configuration file. You can then easily replace a collaboration object with a similar interface when needed. }
The Spring framework is a layered architecture consisting of 7 well-defined modules. The Spring module is built on top of the core container, and the core container defines how the bean is created, configured, and managed.
Each module (or component) that makes up the Spring framework can exist either independently or jointly with one or more other modules. The functions of each module are as follows:
☆ Core Containers: Core containers provide the basic functionality of the Spring framework. The main component of the core container is Beanfactory, which is the implementation of the factory pattern. Beanfactory uses control reversal (IOC) mode to separate the application's configuration and dependency specifications from the actual application code.
☆spring Context: The spring context is a configuration file that provides contextual information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, checksums, and scheduling capabilities.
☆spring AOP: With configuration management features, the Spring AOP module integrates aspect-oriented programming capabilities directly into the spring framework. Therefore, it is easy to make any object managed by the Spring framework support AOP. The spring AOP module provides transaction management services for objects in a spring-based application. By using Spring AOP, you can integrate declarative transaction management into your application without relying on EJB components. The
☆spring dao:jdbc DAO Abstraction Layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages that are thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (for example, opening and closing connections). The Spring DAO's JDBC-oriented exception conforms to the common DAO exception hierarchy. The
☆spring orm:spring Framework inserts several ORM frameworks, providing an ORM object-relational tool that includes JDO, Hibernate, and IBatis SQL maps. All of these conform to Spring's common transaction and DAO exception hierarchy.
☆spring Web module: The Web context module is built on top of the application context module, providing a context for web-based applications. So, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the work of processing multiple-part requests and binding request parameters to domain objects.
☆spring MVC framework: The MVC Framework is a fully functional MVC implementation that builds WEB applications. With the policy interface, the MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, IText, and POI.
The Spring framework features can be used in any Java EE server, and most of the functionality also applies to an environment that is not managed. The central point of Spring is to support reusable business and data access objects that are not bound to a specific Java EE service. There is no doubt that such objects can be reused in different Java EE environments (Web or EJB), stand-alone applications, and test environments.
IOC and AOP
The basic concept of control reversal mode (also known as dependency intervention) is to not create objects, but to describe how they are created. You do not connect directly to objects and services in your code, but describe in a configuration file which of the components you need. The container, which is the IOC container in the Spring framework, is responsible for linking these together.
In a typical IOC scenario, the container creates all the objects and sets the necessary properties to connect them together, deciding what time to call the method. The following table lists an implementation pattern for IOC.
The Spring framework's IOC container is implemented with type 2 and type 3.
Aspect-oriented programming
Aspect-oriented programming, AOP, is a programming technique that allows programmers to modularize behavior, such as logging and transaction management, of crosscutting concerns or crosscutting boundaries of responsibility. The core structure of AOP is the aspect that encapsulates the behavior that affects multiple classes into reusable modules.
AOP and IOC are complementary technologies that use a modular approach to solve complex problems in enterprise application development. In a typical object-oriented development approach, you might want to place logging statements in all methods and Java classes to achieve logging functionality. In the AOP approach, the logging services can be modularized in turn and applied declaratively to the components that require logging. The advantage, of course, is that the Java class does not need to know the existence of the log service, nor does it need to consider the relevant code. So, the application code written with Spring AOP is loosely coupled.
The capabilities of AOP are fully integrated into the context of Spring transaction management, logging, and various other features.
IOC container
The core of Spring design is the Org.springframework.beans package, which is designed to work with JavaBean components. This package is usually not used directly by the user, but is used by the server as the underlying intermediary for most other functions. The next most advanced abstraction is the Beanfactory interface, which is the implementation of the factory design pattern, allowing objects to be created and retrieved by name. Beanfactory can also manage the relationships between objects.
The beanfactory supports two object models.
-a single State model provides a shared instance of an object with a specific name and can be retrieved at query time. Singleton is the default and most commonly used object model. Ideal for stateless service objects.
-The prototype model ensures that separate objects are created for each retrieval. The prototype model works best when each user needs his or her own object.
The Bean Factory concept is the foundation of Spring as an IOC container. The IOC shifts responsibility for handling things from application code to framework. As I will demonstrate in the next example, the Spring framework uses the JavaBean attribute and configuration data to indicate the dependencies that must be set.
Beanfactory interface
Because Org.springframework.beans.factory.BeanFactory is a simple interface, it can be implemented for a variety of underlying storage methods. The most commonly used beanfactory definition is xmlbeanfactory, which loads the bean based on the definition in the XML file, as shown in Listing 1.
Listing 1. Xmlbeanfactory
Beanfactory factory = new Xmlbeanfactory (New Fileinputsteam ("Mybean.xml"));
The bean defined in the XML file is negatively loaded, which means that the bean itself will not be initialized until the bean is needed. To retrieve a bean from beanfactory, simply call the Getbean () method and pass in the name of the bean you want to retrieve, as shown in Listing 2.
Listing 2. Getbean ()
Mybean Mybean = (Mybean) factory.getbean ("Mybean");
Each bean's definition can be POJO (defined by the class name and JavaBean initialization property) or Factorybean. The Factorybean interface adds an indirect level to the application built using the Spring framework.
IOC Example
The easiest way to understand control reversal is to look at its actual application. In the 1th part of the three-part Spring series, I used an example that showed how to inject the application dependencies through the Spring IOC container (rather than building them in).
I use the use case that opens the online credit account as the starting point. For this implementation, opening a credit account requires the user to interact with the following services:
☆ Credit Rating Service, query the user's credit history information.
☆ Remote Information Link service, insert customer information, connect customer information with credit card and bank information for automatic debit (if required).
☆ e-Mail service that sends e-mail messages about the status of a credit card to the user.
Three interfaces
For this example, I assume that the service already exists, ideally integrating them in a loosely coupled way. The following checklist shows the application interfaces for three services.
Listing 3. Creditratinginterface
Public interface Creditratinginterface {public
boolean getusercredithistoryinformation (Icustomer icustomer);
}
The credit rating interface, shown in Listing 3, provides credit history information. It requires a customer object that contains account information. The implementation of this interface is provided by the Creditrating class.
Listing 4. Creditlinkinginterface
Public interface Creditlinkinginterface {public
String getUrl ();
public void SetUrl (String URL);
public void Linkcreditbankaccount () throws Exception;
}
The credit link interface connects the credit history information with the bank information (if required) and inserts the user's credit card information. The credit link interface is a remote service, and its query is done through the GETURL () method. The URL is set by the bean configuration mechanism of the Spring framework and I'll discuss it later. The implementation of this interface is provided by the Creditlinking class.
Listing 5. Emailinterface
Public interface Emailinterface {public
void SendEmail (Icustomer icustomer);
Public String getfromemail ();
public void Setfromemail (String fromemail);
Public String GetPassword ();
public void SetPassword (String password);
Public String getsmtphost ();
public void Setsmtphost (String smtphost);
Public String GetUserID ();
public void Setuserid (String userId);
I hope this article will help you with Java programming.