Common problems and solutions of SSH framework

  • 2020-04-01 01:31:40
  • OfStack

How does Hibernate work and why?
Principle:
1. Read and parse the configuration file
2. Read and parse the mapping information to create a SessionFactory
3. Open the Sesssion
4. Create transaction Transation
5. Persist operations
6. Commit transactions
7. Close the Session
8. Close SesstionFactory
Why use:
1. The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer.
2. Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. It greatly simplifies the coding of the DAO layer
Hibernate USES Java reflection instead of bytecode enhancers to achieve transparency.
Hibernate performs very well because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.
2. How does Hibernate lazily load?
1. Hibernate2 lazy load implementation: a) entity object b) Collection
2. Hibernate3 provides lazy loading of properties
When Hibernate queries data, the data does not exist in memory. When the program actually operates on the data, the object exists in memory, which realizes lazy loading. It saves the memory overhead of the server and thus improves the performance of the server.
3. How to realize the relationship between classes in Hibernate? (e.g. One-to-many, many-to-many)
The relationships between classes are mainly reflected in the relationships between tables. They operate on objects. We map all the tables and classes together in our program.
4. Talk about Hibernate's caching mechanism
1. Internal cache exists in Hibernate, also called first-level cache, which belongs to the application event-level cache
2. Level 2 cache:
A) application and caching
B) distributed caching
Conditions: the data will not be modified by the third party, the data size is 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 cache
5. Hibernate query mode
Sql, the Criteria, the object comptosition
Hql:
1. Attribute query
2, parameter query, named parameter query
3. Correlation query
4. Paging queries
5. Statistical function
6. How to optimize Hibernate?
1. Use bidirectional one-to-many association instead of one-way one-to-many
2. Use one-way one-to-many associations flexibly
3. Instead of one-to-one, use many-to-one
4. Configure the object cache instead of using the collection cache
5. Use Bag for one-to-many sets and Set for many-to-many sets
6. Inheritance classes use explicit polymorphism
7. The table field should be less, the table association should not be afraid of more, there are two levels of cache support
7. Struts working mechanism? Why use Struts?
Working mechanism:
Struts workflow:
In the web application startup initialization will load the ActionServlet, the ActionServlet from
The struts-config.xml file reads the configuration information and stores it in various configuration objects
When the ActionServlet receives a customer request, the following process is performed.
-(1) retrieve the ActionMapping instance matching the user's request, and return invalid information of the request path if it does not exist;
-(2) if the ActionForm instance does not exist, create an ActionForm object to save the form data submitted by the customer into the ActionForm object;
-(3) determine whether form validation is required based on configuration information.
- (4) if actionforms validate () method returns a null or return a does not contain ActionMessage ActuibErrors object, it means the forms authentication success;
-(5) the ActionServlet decides to forward the request to which Action based on the mapping information contained in ActionMapping. If the corresponding Action instance does not exist, it will first create this instance and then call the execute() method of the Action;
-(6) the execute() method of the Action returns an ActionForward object, and the ActionServlet forwards the client request to the JSP component pointed by the ActionForward object;
-(7)ActionForward object generates a dynamic web page to the JSP component and returns it to the customer;
Why use:
The advent of JSP, Servlet, and JavaBean technologies made it possible to build powerful enterprise applications. But the systems built with these technologies were very messy, so on top of that, we needed a rule, a rule to organize these technologies, and that was the framework, and Struts was born.
The application developed based on Struts consists of three types of components: controller component, model component and view component
8. How does Struts' validate framework validate?
Configure the specific error prompt in the struts configuration file, and then call the validate() method in the FormBean.
9. Talk about the Struts design pattern
MVC pattern: the web application loads and initializes the ActionServler when it starts. When the user submits the form, a configured ActionForm object is created and filled in the corresponding data of the form. ActionServler determines whether form validation is needed according to the configured Settings of struts-config.xml file. If so, it invokes the Validate () validation of ActionForm and then selects which Action to send the request to. The execute () method of the Action is then called. Execute () gets the data from the ActionForm object, completes the business logic, returns an ActionForward counter, the ActionServlet forwards the customer request to the JSP component specified by the ActionForward object, and the JSP specified by the ActionForward object generates the dynamic web page and returns it to the customer.
10. Spring works and why?
Spring MVC requests that all requests be submitted to the DispatcherServlet, which delegates the other modules of the application system to do the actual processing of the request.
2. The DispatcherServlet queries one or more HandlerMapping to find the Controller that handles the request.
3. The DispatcherServlet requests to be submitted to the target Controller
4. After the Controller handles the business logic, it will return a ModelAndView
5.Dispathcher queries one or more ViewResolver view parsers to find the view object specified by the ModelAndView object
6. The view object is responsible for rendering back to the client.
Why use:
{AOP allows developers to create non-behavioral concerns, called crosscutting concerns, and insert them into application code. With AOP, common services (such as logging, persistence, transactions, and so on) can be decomposed into aspects and applied to domain objects without increasing the complexity of the object model for domain objects.
IOC allows you to create an application environment that can construct objects and then pass their collaboration objects to those objects. As the word inversion indicates, IOC is like JNDI in reverse. Instead of a bunch of abstract factories, service locators, singletons, and straight construction, each object is constructed with its collaboration object. So it is the container that manages the collaborator.
Spring, even as an AOP framework, is an IOC container. The best thing about Spring is that it helps you replace objects. With Spring, you simply add dependencies (collaboration objects) with javabeans properties and configuration files. Collaboration objects with similar interfaces can then be easily replaced as needed. }
The Spring framework is a layered architecture consisting of seven well-defined modules. The Spring modules are built on top of the core container, which defines how beans are created, configured, and managed, as shown in figure 1.
Each module (or component) that makes up the Spring framework can exist separately, or be implemented in conjunction with one or more other modules. The functions of each module are as follows:
Do things core container: the core container provides the basic functionality of the Spring framework. The main component of the core container is the BeanFactory, which is an implementation of the factory pattern. The BeanFactory USES the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
Do things Spring context: a Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, ejbs, E-mail, internationalization, and validation and scheduling capabilities.
Do things Spring AOP: through configuration management features, the Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework. As a result, it is easy to make any object managed by the Spring framework AOP enabled. The Spring AOP module provides transaction management services for objects in spring-based applications. By using Spring AOP, you can integrate declarative transaction management into your applications without relying on EJB components.
Do things Spring DAO: the JDBC DAO abstraction layer provides a meaningful exception hierarchy to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written (such as opening and closing connections). Spring DAO's jdbc-oriented exceptions follow the common DAO exception hierarchy.
Do things Spring ORM: the Spring framework inserts several ORM frameworks to provide ORM's object-relational tools, including JDO, Hibernate, and iBatis SQL Map. All of this complies with Spring's generic transaction and DAO exception hierarchies.
Do things Spring Web module: the Web context module is built on top of the application context module to provide context for web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies processing multipart requests and binding request parameters to domain objects.
Do things Spring MVC framework: the MVC framework is a full-featured MVC implementation for building Web applications. The MVC framework becomes highly configurable through the policy interface, and MVC houses a large number of view technologies, including JSP, Velocity, Tiles, iText, and POI.
The capabilities of the Spring framework can be used in any J2EE server, and most of them are also suitable for unmanaged environments. At the heart of Spring is support for reusable business and data access objects that are not bound to specific J2EE services. Undoubtedly, such objects can be reused between different J2EE environments (Web or EJB), standalone applications, and test environments.
The IOC and AOP
The basic concept of the inversion of control pattern (also known as dependency intervention) is not to create objects, but to describe how to create them. It is not directly wired to objects and services in the code, but describes which components need which services in the configuration file. The container (in the Spring framework, the IOC container) is responsible for tying these things together.
In a typical IOC scenario, the container creates all the objects, sets the necessary properties to wire them together, and decides when to invoke the method. The following table lists an implementation pattern for IOC.
The Spring framework's IOC container is implemented in type 2 and type 3.
Aspect-oriented programming
Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns or behavior that crosscuts typical lines of responsibility, such as logging and transaction management. The core construct of AOP is the aspect, which encapsulates the behavior that affects multiple classes into reusable modules.
AOP and IOC are complementary technologies that both take a modular approach to solving complex problems in enterprise application development. In a typical object-oriented development approach, logging might be implemented by placing logging statements in all methods and Java classes. In an AOP approach, log services can be modularized in turn and applied declaratively to components that require logging. The advantage, of course, is that the Java class doesn't need to know about the existence of the logging service and doesn't need to think about the associated code. As a result, application code written with Spring AOP is loosely coupled.
AOP's capabilities are fully integrated into the context of Spring transaction management, logging, and various other features.
The IOC container
Spring design is the core of the org. Springframework. Beans bag, its design goal is to use with JavaBean component. This package is typically not used directly by the user, but is used by the server as an underlying mediation for most other functions. The next highest level abstraction is the BeanFactory interface, an implementation of the factory design pattern that allows objects to be created and retrieved by name. The BeanFactory can also manage relationships between objects.
The BeanFactory supports two object models.
- the singleton model provides a Shared instance of an object with a specific name that 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 a separate object is created for each retrieval. The prototype model works best when each user needs their own object.
The concept of a bean factory is the basis for Spring as an IOC container. The IOC shifts the responsibility for doing things from the application code to the framework. As I will demonstrate in the next example, the Spring framework USES JavaBean properties and configuration data to indicate the dependencies that must be set.
The BeanFactory interface
Because org. Springframework. Beans. Factory. The BeanFactory is a simple interface, so it can be for a wide variety of underlying storage method. The most common 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 beans defined in the XML file are loaded negatively, which means that the beans themselves are not initialized until they are needed. To retrieve the bean from the BeanFactory, simply call the getBean() method and pass in the name of the bean to be retrieved, as shown in listing 2.
Listing 2. The getBean ()
MyBean MyBean = (MyBean) factory.getbean (" MyBean ");
Each bean can be defined as a POJO (property definition with class name and javabeans initialization) or a FactoryBean. The FactoryBean interface adds an indirect level to applications built using the Spring framework.
The IOC sample
The easiest way to understand inversion of control is to see it in action. In concluding part 1 of the three-part Spring series, I used an example to demonstrate how to inject application dependencies through the Spring IOC container (rather than building them in).
I use the use case for opening an online credit account as a starting point. For this implementation, opening a credit account requires the user to interact with the following services:
Do things credit rating service, inquiry user's credit history information.
Do things remote information link service to insert customer information and connect customer information with credit card and bank information for automatic debit (if necessary).
Do things an E-mail service that sends users e-mails about the status of their credit CARDS.
Three interfaces
For this example, I assume that the services already exist, and ideally integrate them in a loosely coupled manner. The following listing shows the application program interfaces for the 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 Customer 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 credit history information with bank information, if needed, and inserts the user's credit card information. The credit link interface is a remote service that queries through the getUrl() method. The URL is set by the Spring framework's bean configuration mechanism, which I'll discuss 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);

Related articles: