Working principle and process of the three SSH frameworks

Source: Internet
Author: User
Tags server memory to domain
How does hibernate work and why?

Principle:
1. Read and parse the hibernate. cfg. xml configuration file through configuration (). Configure ();
2. The ing information is read and parsed by <Mapping Resource = "com/XX/user. HBM. xml"/> In hibernate. cfg. xml.
3. Create sessionfactory through config. buildsessionfactory (); //
4. sessionfactory. opensession (); // open sesssion
5. session. begintransaction (); // create transaction Transation
6. Persistent operate persistent operation
7. session. gettransaction (). Commit (); // submit the transaction
8. Disable session
9. Disable sesstionfactory

Why:
1. The code of the JDBC connector database is encapsulated, which greatly simplifies the tedious and repetitive code of the data connector layer.

2. Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. He greatly simplifies the coding of the DaO layer.

3. hibernate uses the Java reflection mechanism instead of the bytecode enhancement program to achieve transparency.

4. hibernate has excellent performance because it is a lightweight framework. The flexibility of ing is excellent. It supports various relational databases, from one-to-one to many-to-many complex relationships.
 
2. How does hibernate delay loading?
1. Implementation of hibernate2 delayed loading: a) entity object B) Collection)

2. hibernate3 provides the attribute delayed Loading Function

When hibernate queries data, the data does not exist in the memory. When the program actually operates on the data, the object exists in the memory, which enables delayed loading, it saves server memory overhead and improves server performance.

3. How to Implement the relationship between classes in hibernate? (For example, one-to-many and many-to-many relationships)

The relationship between classes is mainly reflected in the relationship between tables. They operate on objects in a city. In our program, all tables and classes are mapped together, they use the following methods in the configuration file: Upload-to-one, one-to-least, upload-to-least,

4. Talk about the cache mechanism of Hibernate

1. The internal cache exists in hibernate, which is also called a level-1 cache, which belongs to the application thing-level cache.

2. Secondary cache:
A) Application and Cache
B) distributed cache
Condition: the data is not changed by a third party, the data size is acceptable, the data update frequency is low, the same data is frequently used by the system, and the data is not important.
C) third-party cache implementation
/////// Level 1 cache: Session-level cache is also called transaction-level cache. It only caches entities, and the lifecycle is consistent with that of sessions. It cannot be managed.

Do not display the call.

Level-2 Cache: sessionfactory cache, also called process-level cache. It is implemented using a 3rd-party plug-in and also a value-based Cache entity. The lifecycle is consistent with sessionfactory and can be managed.

First, configure the 3rd plug-in. We use ehcache and add it to the hibernate. cfg. xml file?

<Property name = "hibernate. cache. user_second_level_cache"> true </property>

Call to be displayed in the ing, <Cache Usage = "read-only"/>

Second-level cache query cache: caches common attributes. If the associated table is modified, the lifecycle of the query cache is also over.

You must manually enable query cache in the program: Query. setcacheable (true );/////////

5. Hibernate query method
SQL, criteria, object comptosition
Hql:
1. query attributes
2. query the number of partitions and query the number of named partitions
3. join query
4. Paging Query
5. Statistical functions

6. How to optimize hibernate?
1. Use bidirectional one-to-Multiple Association instead of unidirectional one-to-Multiple Association
2. flexible use of one-to-multiple associations
3. Do not use one-to-one instead of multiple-to-one
4. Configure the object cache without using the set Cache
5. Use bag for one-to-multiple sets and set for multiple-to-multiple sets
6. Use explicit polymorphism for inheritance classes
7. Fewer table fields, no more table Association, and secondary cache support


1. What data query methods does hibernate use?

(1) navigation object graph Query

(2) oId Query

(3) hql

(4) QBC

(5) Local SQL

2. Differences between load () and get ()

 

Load loading method:

Java code

Users user = (users) Session. Load (users. Class, userid );

Users user = (users) Session. Load (users. Class, userid );

Get loading method:

Java code

Users user = (users) Session. Get (users. Class, userid );

Users user = (users) Session. Get (users. Class, userid );

 

Differences between the two loading methods:

Difference 1: Assume that there is no userid object in the database. If the get method is used for loading, a null value is returned. If the load method is used for loading, a proxy object is returned. If the following code assumes that a certain attribute of the user object is called (for example, user. getPassword () throws an exception: Org. hibernate. objectnotfoundexception;

Difference 2: Load supports delayed loading, while get does not support delayed loading.

That is to say:

Java code

Users user = (users) Session. Load (users. Class, userid );

Users user = (users) Session. Load (users. Class, userid );

This Code does not run the database query. It only runs the database query when it is used by the user.

And:

Java code

Users user = (users) Session. Get (users. Class, userid );

Users user = (users) Session. Get (users. Class, userid );

Then run the database query immediately. Therefore, users user = (users) Session. Load (users. Class, userid); does not run any SQL statement.

Note:

Java code

Users user = (users) Session. Load (users. Class, userid );
System. Out. println (user. GETID ());

Users user = (users) Session. Load (users. Class, userid );

System. Out. println (user. GETID ());

The above two statements do not run database operations. Since a map object will be stored in the hibernate level-1 cache after load, the key of the map is the value of userid, but when you GETID, it takes the map key value in the first-level cache instead of running the database query. Therefore, no error is reported. It does not run any database operation.

 
Describes the running process of struts2. The struts 2 framework can be roughly divided into three parts: the core controller filterdispatcher, the Business controller action, and the enterprise business logic component implemented by the user. The core controller filterdispatcher is the basis of the struts 2 framework, including the internal control process and processing mechanism. The business controller action and business logic components must be implemented by the user. When developing the same action and business logic components, you also need to write related configuration files for the core controller filterdispatcher to use. The workflow of struts 2 is simpler than that of struts 1, which is basically the same as that of the webwork framework. Therefore, Struts 2 is the upgraded version of webwork. The basic process is as follows: 1. The client browser sends an HTTP request. 2. According to the Web. xml configuration, this request is received by filterdispatcher. 3. According to struts. xml configuration, find the action class and method to be called, and inject the value to aciton through IOC. 4. Action calls the business logic component to process the business logic. This step includes form verification. 5. After the action is completed, find the returned result based on the configuration in struts. xml and go to the corresponding page. 6. Return an HTTP Response to the client browser.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/alli0968/archive/2009/06/01/4231237.aspx

 

 

. Struts working mechanism? Why use struts?
Working mechanism:
Struts workflow:
When the web application is started, the initialization actionservlet is loaded, and the actionservlet starts from
Read configuration information in the struts-config.xml file and store them to various configuration objects
When an actionservlet receives a customer request, it runs the following process.
-(1) retrieve the actionmapping instance that matches the user request. If the instance does not exist, the invalid request path information is returned;
-(2) if the actionform instance does not exist, create an actionform object and save the form data submitted by the customer to the actionform object;
-(3) determine whether form verification is required based on the configuration information. If verification is required, the actionform's validate () method is called;
-(4) assume that the validate () method of actionform returns NULL or an actuiberrors object that does not include actionmessage, it indicates that the form verification is successful;
-(5) The actionservlet determines the action to which the request is forwarded based on the actioning information included in actionmapping. If the corresponding action instance does not exist, create the instance and then call the action's execute () method;
-(6) The execute () method of action returns an actionforward object. The actionservlet is forwarding the client request to the JSP component pointed to by the actionforward object;
-(7) The actionforward object directs to the JSP component to generate a dynamic webpage and return it to the customer;

Why:
The emergence of JSP, Servlet, and JavaBean technologies makes it possible for us to build a powerful enterprise application system. However, systems built with these technologies are messy. Therefore, we need a rule and a rule to organize these technologies. This is the framework, and Struts came into being.

Struts-based applications are composed of three types of components: controller components, model components, and view components.

8. How is struts's validate framework verified?
Configure detailed error messages in the struts configuration file, and then call the validate () method in formbean in detail.

9. Let's talk about the struts design model.
MVC mode: When a web application is started, it loads and initializes actionservler. When a user submits a form, a configured actionform object is created and filled with the data corresponding to the form, actionservler determines whether form verification is required based on the settings configured in the Struts-config.xml file, assume that you need to call the validate () Verification of actionform and select the action to which the request is sent. If the action does not exist, actionservlet will first create this object and then call the execute () method of action. Execute () obtains data from the actionform object. After the business logic is completed, an actionforward object is returned. The actionservlet then forwards the customer request to the JSP component specified by the actionforward object, the JSP specified by the actionforward object generates a dynamic webpage and returns it to the customer.

10. Why does spring work?
1. Spring MVC requests all requests are submitted to dispatcherservlet. It will entrust other modules of the application system to handle the requests in real time.
2. The dispatcherservlet queries one or more handlermapping and finds the Controller that processes the request.
3. Submit the dispatcherservlet request to the target controller.
4. After the controller processes the business logic, a modelandview is returned.
5. The dispathcher queries one or more viewresolver view Resolvers and finds the view object specified by the modelandview object.
6. view objects are rendered and returned to the client.

Why:
{AOP allows developers to create non-behavioral concerns, known as cross-cutting concerns, and insert them into application code. After using AOP, public services (such as logs, persistence, transactions, etc.) can be decomposed into aspects and applied to domain objects. At the same time, they will not be added? Complexity of the Object Model of the domain object.
IOC agrees to create an application environment that can construct objects and then pass their collaboration objects to these objects. As the word inversion indicates, IOC is like the reversed JNDI. Instead of using a bunch of Abstract Factory, service locator, Singleton, and straight construction, each object is constructed with its collaborative object. Therefore, collaborator is managed by containers ).
Even an AOP framework, spring is also an IOC container. The best thing about spring is that it helps you replace objects. With Spring, should I only add the JavaBean attribute and configuration file? Dependency (collaboration object ). Then, you can easily replace the collaboration object with similar interfaces when necessary .}

 


The Spring framework is a layered architecture consisting of seven well-defined modules. The spring module is built on the core container. The core container defines how to create, configure, and manage beans, as shown in figure 1.

Each module (or component) that makes up the Spring framework can exist independently or be implemented together with one or more other modules. The functions of each module are as follows:

☆Core container: the Core container provides the basic functions of the Spring framework. The main component of the core container is beanfactory, which is the implementation of the factory mode. Beanfactory uses the control reversal (IOC) mode to separate application configuration and dependency specifications from actual application code.

☆Spring context: Spring context is a configuration file that provides context information to the Spring framework. Spring context includes enterprise services, such as JNDI, EJB, email, internationalization, checksum and scheduling.

☆Spring AOP: Through the configuration management feature, the Spring AOP module directly integrates Aspect-oriented programming functions into the spring framework. Therefore, it is very easy to make Spring framework management support AOP for any object. 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 applications without relying on EJB components.

☆Spring Dao: The JDBC Dao abstraction layer provides a meaningful exception hierarchy for managing Exception Handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of Exception Code to be written (such as opening and closing connections ). JDBC-oriented exceptions of spring Dao follow the common Dao exception hierarchy.

☆Spring ORM: The Spring framework inserts several ORM frameworks to provide ORM Object Relational tools, including JDO, hibernate, and ibatis SQL map. All of these comply with the general transactions and Dao exception hierarchies of spring.

☆Spring web module: The Web context module is built on the application context module and provides context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The web module also simplifies the process of processing multiple requests and binding the number of requests to the domain object.

☆Spring MVC framework: the MVC Framework is a full-featured MVC implementation for Building Web applications. Through the policy interface, the MVC framework becomes highly configurable. MVC supports a large number of view technologies, including JSP, velocity, tiles, itext, and poi.

The functions of the Spring framework can be used in whatever J2EE server, and most of the functions are also applicable to unmanaged environments. The core point of spring is that it supports reusable business and data issue objects that are not bound to a specific J2EE service. There is no doubt that such objects can be reused between different J2EE environments (web or EJB), independent applications, and trial environments.

IOC and AOP

The basic concept of the inversion control mode (also known as dependency intervention) is to describe how to create objects without creating objects. The Code does not directly connect to objects and services, but the configuration file describes which component requires which service. Containers (IOC containers in the Spring framework) are responsible for connecting these together.

In typical IOC scenarios, the container creates all objects and sets the necessary properties to connect them together to determine the time to call the method. The following table lists an implementation mode of IOC.

 


The IOC container runtime of the Spring framework is implemented by Type 2 and Type 3.


Aspect-Oriented Programming

Aspect-Oriented Programming, that is, AOP, is a programming technique that allows programmers to modularize the behavior of cross-concern or cross-common division of duties (such as logging and transaction management. The core structure of AOP is aspect, which encapsulates the behaviors that affect multiple classes into reusable modules.

AOP and IOC are complementary technologies that use modular methods to solve complex problems in enterprise application development. In a typical object-oriented development method, you may need to place the log record statement in all methods and Java classes to implement the log function. In the AOP mode, the log service can be modularized in turn and applied to the components that require logs in declarative mode. Of course, the advantage is that the Java class does not need to know the existence of the log service, nor need to consider the relevant code. Therefore, application code written with Spring AOP is loosely coupled.

The functions of AOP are fully integrated into the context of Spring transaction management, logs, and other features.

IOC container

The core of spring design is the org. springframework. Beans package, which is designed to be used with the JavaBean Component. This package is usually not directly used by users, but is used by the server as the underlying intermediary for most other functions. The next highest level abstraction is the beanfactory interface, which is the implementation of the factory design pattern and agrees to create and retrieve objects by name. Beanfactory can also manage relationships between objects.

Beanfactory supports two object models.

□The single-State model provides shared instances of objects with specific names, which can be searched during query. Singleton is the default and most frequently used object model. It is ideal for stateless service objects.

□The prototype model ensures that a separate object is created for each retrieval. The prototype model is the most suitable for all users who need their own objects.

The concept of Bean Factory is that spring serves as the basis of IOC containers. IOC transfers the responsibility for handling tasks from application code to the framework. As I will demonstrate in the next demo, the Spring framework uses the JavaBean attribute and configuration data to indicate the dependencies that must be set.

Beanfactory Interface

Since org. springframework. Beans. Factory. beanfactory is a simple interface, it can be implemented for various underlying storage methods. The most frequently used beanfactory definition is xmlbeanfactory, which loads beans according to the definition in the XML file, as shown in figure 1.

Listing 1. xmlbeanfactory

Beanfactory factory = new xmlbeanfactory (New fileinputsteam ("mybean. xml "));

The bean defined in the XML file is passively loaded, which means that the bean itself will not be initialized before the bean is required. To retrieve beans from beanfactory, you only need to call the getbean () method and input the name of the bean to be retrieved, as shown in figure 2.

Listing 2. getbean ()

Mybean = (mybean) Factory. getbean ("mybean ");

Every bean can be pojo (defined by class name and JavaBean initialization attribute) or factorybean. How does the factorybean interface add to applications built using the Spring framework? An indirect level.

IOC demo

The simplest way to understand control reversal is to look at its practical application. I used a demo to summarize Part 1 of the three-part Spring Series, demonstrate how to inject application dependencies through the Spring IoC container (instead of building them in ).

I started with an online credit account use case. To enable a credit account, you must interact with the following services:

☆Credit level evaluation service, which queries users' credit history information.

☆Remote Information Link Service, insert customer information, connect customer information with credit card and bank information, and take the initiative to debit (if necessary ).

☆Email Service sends an email about the credit card status to the user.

Three Interfaces

For this demo, if the service already exists, the ideal situation is to integrate them in a loosely coupled manner. The following list shows the application interfaces of the three services.

Listing 3. creditratinginterface

Public interface creditratinginterface {
Public Boolean getusercredithistoryinformation (icustomer );
}

The credit level evaluation interface shown in listing 3 provides credit history information. It requires a customer object that includes 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 the credit history information with the bank information (if necessary) and inserts the user's credit card information. The credit Link interface is a remote service and its query is carried out through the geturl () method. The URL is set by the bean configuration mechanism of the Spring framework. I will discuss it later. The implementation of this interface is provided by the creditlinking class.

Listing 5. emailinterface

Public interface emailinterface {

Public void sendemail (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 );

Reprinted please indicate the source: http://www.cnblogs.com/javaNewegg/archive/2011/08/28/2156521.html

Working principle and process of the three SSH frameworks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.