Java Programming → Struts → struts2 → 2010-02-05
Struts, Hibernate, Spring face questions
Hibernate works and why to use?
1. To read and parse the configuration file
2. To read and parse the mapping information, create a SessionFactory
3. Open Sesssion
4. Create Service Transation
5. Persistence operations
6. Commits the transaction
7. Close Session
8. Close SesstionFactory
1. On the JDBC code to access the database to do the package, greatly simplifies the tedious repetitive data access layer code.
2. Hibernate is a JDBC-based persistence framework of the mainstream, is an excellent ORM implementation. He very much simplified coding DAO layer
3. Hibernate using the Java reflection mechanism, instead of byte code enhancement process to achieve transparency.
4. Hibernate performance is very good, because it is a lightweight framework. The flexibility of mapping is very good. It supports a variety of relational database, from one-to-many relationship between the various complex.
2. Hibernate is to delay load?
1. Hibernate2 lazy to achieve: a) physical objects b) a collection (Collection)
2. Hibernate3 provides lazy functional properties
When the Hibernate query data Di when the data did not exist and memory Zhong, when the program's operation, the real data, the object to exist and memory, In respect of realized the delay load, save him the Neicunkaixiao the server, thereby increasing server performance.
3. How to Hibernate in the relationship between the implementation class? (Eg: one to many, many to many relationship)
The relationship between class and class is mainly reflected in the table and the relationship between the operating table, which operates the city on the object, we process all the tables and classes are mapped together, they file through the configuration many-to -one, one-to-many, many-to-many,
4. Under the Hibernate's caching mechanism that
1. Hibernate in the internal cache is also called a cache there, things are application-level cache
2. 2 cache:
a) Application and Cache
b) distributed cache conditions: data will not be third-party modification, the size of the acceptable range of data, data update frequency is low, the same data frequently used by the system, non-critical data
c) the realization of third-party cache
5. Hibernate's query method
Sql, Criteria, object comptosition
1, attribute query
2, parameter query, named parameter query
3, related queries
4, page query
5, statistical functions
6. How to Optimize Hibernate?
1. Using two-way-to-many association, do not use one-way-to-many
2. A flexible one-way-to-many association
3. No-one, with many to one instead
4. Configure object cache, the cache does not use a collection
5. To-many set to use Bag, a collection of use-many Set
6. Inherited class explicit polymorphism
7. Table field to a small, multi-table association should not be afraid, there are two cache support
7. Struts working mechanism? Why should I use Struts?
Struts work flow:
In the web applications will be loaded at startup initialization ActionServlet, ActionServlet from
struts-config.xml file to read configuration information, store them in a variety of configurations to object when the ActionServlet receives a client request, will perform the following process.
- (1) search and match the user request ActionMapping instance, if you do not exist, it returns invalid path requested information;
- (2) If the ActionForm instance does not exist, we create a ActionForm object, save the form data submitted by customers to the ActionForm object;
- (3) According to the configuration information for deciding whether to form validation. If you need to verify, on the call ActionForm's validate () method;
- (4) If the ActionForm's validate () method returns null or return an object that does not contain ActionMessage the ActuibErrors, on that form validation is successful;
- (5) ActionServlet under ActionMapping mapping information contained in the decision to forward the request to which Action, Action, if the corresponding instance does not exist, create the first instance, then call the Action's execute () method;
- (6) Action's execute () method returns an ActionForward object, ActionServlet forwards client requests in the ActionForward object to point to the JSP components;
- (7) ActionForward object point to JSP component to generate dynamic web pages, returned to the client;
JSP, Servlet, JavaBean technology appears to us to build powerful enterprise applications possible. However, systems built using these technologies is very complicated mess, on top of this, we need a rule, a technical organization up these rules, this is the framework, Struts will come into being.
Struts-based applications developed by the Category 3 components: the controller components, model components, view components
8. The validate Struts framework is how to verify it?
In the struts config file specific error, then in FormBean the validate () method specific call.
9. Under the Struts design patterns that
MVC pattern: web application starts it will load and initialize ActionServler. When the user submits the form, a configured ActionForm object is created, the corresponding data form was filled, ActionServler under Struts-config.xml file configuration settings determine the need for good form validation, if necessary to call the ActionForm's Validate () validated the option to send the request to which Action, if there is no Action, ActionServlet will first create the object, then call the Action's execute () method. Execute () to obtain data from the ActionForm object to complete the business logic, the object returns an ActionForward, ActionServlet then client requests to the specified ActionForward object jsp component, ActionForward object specified jsp generate dynamic web pages, returned to the customer.
10. spring mechanisms work and why to use?
1.spring mvc please all requests are submitted to the DispatcherServlet, it commissioned the application system for other modules responsible for processing requests for real.
2.DispatcherServlet check one or more HandlerMapping, find the address requested Controller.
3.DispatcherServlet please submit a request to the target Controller
4.Controller for business logic processing, it will return a ModelAndView
5.Dispathcher check one or more ViewResolver view resolver, find the ModelAndView object specified view object
6. View object to return to the client for rendering.
Why to use:
(AOP allows developers to create non-behavioral concerns, called crosscutting concern, and insert them into the application code. Using AOP, the public services (such as logging, persistence, transactions, etc.) can be broken down into aspects and applied to domain objects, domain objects without increasing the complexity of the object model.
IOC allowed to create an application environment objects can be constructed, and then pass these objects to their collaborators. As indicated by the word upside down, IOC just turn the JNDI. Do not use a bunch of abstract factories, service locators, a single element (singleton) and direct construction (straight construction), each object is constructed with its collaborators. So is the container management collaborators (collaborator).
Spring AOP framework even if one is an IOC container. Spring is the best place it can help you replace the object. With the Spring, as long as the use of JavaBean properties and configuration files by adding dependent (collaborators). Can then be easily replaced when required collaboration with a similar interface to the object. )
Spring Framework is a hierarchical structure, well defined by the seven modules. Spring core container module built on top of the container defines the core to create, deploy and manage bean manner shown in Figure 1.
Spring framework composed of each module (or component) can stand alone, or with other joint realization of one or more modules. Each module functions as follows:
☆ core container: The core container provides the basic functions of Spring framework. The main components of the core container is the BeanFactory, it is the factory pattern. BeanFactory to use Inversion of Control (IOC) mode of the application's configuration and dependency specification and the actual application code separate.
☆ Spring context: Spring context is a configuration file, to Spring framework provides context information. Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, validation and scheduling functions.
☆ Spring AOP: through the configuration management features, Spring AOP module directly to the aspect-oriented programming features into the Spring framework. So, you can easily make any Spring managed object framework to support AOP. Spring AOP module for Spring applications based on the object to provide a transaction management service. Through the use of Spring AOP, EJB components do not rely on the declarative transaction management can be integrated into the application.
☆ Spring DAO: JDBC DAO abstraction layer offers a meaningful exception hierarchy, the structure can be used to manage exception handling and different database vendors throw an error message. Exception hierarchy simplifies error handling, and greatly reduces the need to write exception code number (such as opening and closing the connection). Spring DAO JDBC-oriented exceptions comply with the generic DAO exception hierarchy.
☆ Spring ORM: Spring Framework ORM framework into a number, which provides object-relational ORM tool, including JDO, Hibernate, and iBatis SQL Map. All comply with Spring's generic transaction and DAO exception hierarchy.
☆ Spring Web Module: Web context module built on top of the application context module, Web-based applications that provide a context. So, Spring framework supports integration with Jakarta Struts. Web module also streamlined the processing of multi-part requests and to bind request parameters to domain objects work.
☆ Spring MVC framework: MVC framework for building a fully functional Web application MVC implementation. Through strategy interfaces, MVC framework into a highly configurable, MVC view to accommodate a large number of technologies, including JSP, Velocity, Tiles, iText, and POI.
Spring Framework features can be used in any J2EE server, most of the functionality also applies to the unregulated environment. Spring core elements are: support does not bind to specific J2EE services for reusable business and Data Access Objects. There is no doubt that such objects can be in different J2EE environments (Web or EJB), standalone applications, test environments reuse.
IOC and AOP
Inversion of Control pattern (also known as dependency intervention) the basic concept is: do not create objects, but create them in the way described. In the code is not directly connected with the objects and services, but in the configuration file which describes what a component needs a service. Container (in the Spring framework is the IOC container) is responsible for these together.
In a typical IOC scenario, the container creates all objects, and set the necessary properties to connect them together, decide what time to call the method. The following table lists the IOC of an implementation model.
Spring Framework's IOC container with type 2 and type 3 implementation.
Aspect-oriented programming, that is, AOP, is a programming technology that allows programmers to crosscutting concerns or cross-cutting behavior of the typical duties of the line (such as logging and transaction management) module. AOP's core structure is concerned, it will affect the number of classes that encapsulate the behavior of reusable modules.
AOP and IOC are complementary technologies, they all use a modular solution to enterprise application development in the complex. In a typical object-oriented development methods, you may want to log all statements on the Java class methods and logging can be achieved. In AOP mode, you can turn to the log service module, and to declare the way they are applied to the log of the components needed. Of course, the advantage is the Java class does not need to know the existence of the log service does not need to consider the relevant code. Therefore, applications written using Spring AOP is loosely coupled code.
AOP functionality is fully integrated into the Spring transaction management, logging and other features of context.
Spring design is the org.springframework.beans package, its design goal is to be used together with JavaBean components. This package is usually not used directly by the user, but by the server to function as the bottom of most other intermediary. Abstract is the next most senior BeanFactory interface, it is the realization of the factory design pattern that allows to create and retrieve objects by name. BeanFactory can also manage the relationship between objects.
BeanFactory supports two object model.
□ Single-state model provides a specific instance of the shared object name, you can query to retrieve. Singleton is the default and most common object model. For the non-state requirements are very good.
□ prototype model to ensure that each search will create a separate object. In each user needs its own object, the prototype model of the best.
Spring bean factory concept is the basis for IOC container. IOC will be the responsibility of dealing with matters transferred from the application code with the framework. As I will be next, as shown in the example, Spring framework uses JavaBean properties and configuration data that must be set dependencies.
Because org.springframework.beans.factory.BeanFactory is a simple interface, so you can achieve for a variety of underlying storage methods. The most commonly used BeanFactory definition XmlBeanFactory, it is defined according to XML file into bean, as shown in Listing 1.
Listing 1. XmlBeanFactory
BeanFactory factory = new XMLBeanFactory (new FileInputSteam ("mybean.xml"));
Defined in the XML file loaded Bean was negative, which means you need before bean, bean itself will not be initialized. Retrieved from the BeanFactory bean, simply call the getBean () method, pass the bean to retrieve the names can, as shown in Listing 2.
Listing 2. GetBean ()
MyBean mybean = (MyBean) factory.getBean ("mybean");
Each bean definition can be a POJO (JavaBean initialization with the class name and attributes defined) or FactoryBean. FactoryBean interface using Spring framework for building applications that add an indirect level.
Understand the easiest way to control the inversion is to see its practical application. In the Spring by the three-part series Part 1 of a summary, I use an example, demonstrates how Spring IOC container into the application's dependencies (rather than build them come in).
I used to open credit accounts online use cases as a starting point. For the realization of open credit account requires the user to interact with the following services:
☆ credit rating assessment services, check the user's credit history information.
☆ links to remote information services into customer information, customer information and credit card and bank information to connect to the automatic debit (if needed).
☆ mail service to send e-mail credit card status.
For this example, I assume that the service already exists, the ideal situation is to use loosely coupled way to integrate them. The following list shows the three services of the application program interface.
Listing 3. CreditRatingInterface
public interface CreditRatingInterface (
public boolean getUserCreditHistoryInformation (ICustomer iCustomer);
Listing 3 shows the credit rating assessment interface provides credit history information. It requires a Customer object that contains customer information. The interface implementation 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;
Link interface credit history credit information and bank information (if required) to connect together, and insert the user's credit card information. Credit Link interface is a remote service, it queries through getUrl () method of. URL from the Spring bean configuration mechanism to set the framework, I will discuss it. The interface implementation 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);
Source: http://struts2.group.javaeye.com/group/blog/588222, only to learn information.