Struts2 Hibernate Spring running process and Partial Mechanism

Source: Internet
Author: User

Struts2 Hibernate Spring running process and Partial Mechanism
The Tomcat container is loaded and the web. xml corresponding to the webApplication is found.
Start parsing information in web. xml

Read a spring listener and tell the spring container to start initializing the information in the configuration file. The spring container configuration file defaults to applicationContext. xml www.2cto.com.
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener
</Listener-class>
</Listener>

The container starts to instantiate spring-related beans and configuration files,
Spring container creates dataSource data source,
Create sessionFactory of hibernate (input dataSource)
Create the operation template HibernateTemplate required to operate the database (pass in sessionFactory)
And scan and generate the bean (IOC injection) in the configuration file)
Generate each dao input hibernateTemplate
Generate the corresponding dao for each service
(Initialization is complete. Wait for the user's request and only initialize the bean in the spring container)
Spring container Initialization is complete (if action is prototype, not a singleton)
If the action is a Singleton, it will first initialize action, service, dao, hibernateTemplate, sessionFactory, datasource)

One request is sent to tomcat (generating HttpServletRequest)
Tomcat finds a web application based on the url and starts to load the web. xml of the web application.
Read
<Filter>
<Filter-name> struts2 </filter-name>
<Filter-class>
Org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
</Filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts2 </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
Time
1. filter all URLs and send the request to StrutsPrepareAndExecuteFilter (front-end Controller)
Start struts2 work
Struts2 loads the configuration file, including struts-plugin. Xml reading sequence:
Struts-default.xml
Struts-plugin.xml
(In the configuration file in the struts2-spring-plugin.jar
<Constant name = "struts. objectFactory "value =" spring "/>. This configuration specifies that the factory that generates the struts object is generated by spring. If the action is configured with annotations, it is generated by spring at the beginning. If not, it is generated by the struts2 and plugin plug-ins)
Struts. xml
Struts. properties
Web. xml
2. StrutsPrepareAndExecuteFilter calls actionMapper to determine if
Action required
3. If actionMapper determines that the request needs to call an action, then StrutsPrepareAndExecuteFilter
Call the serviceAction method in the Dispatcher class to generate the valuestack (value stack) and actioncontext (action context), and generate the actionproxy object.
Process the request to actionProxy
4. actionProxy view struts. xml through configurationManager
To find all the configuration information (interceptor, converter, etc.) of the action and action to be called)
5. actionProxy creates an actionInvocation instance.
6. actionInvocation uses the command mode to call the configuration file before and after the action
Configure the interceptor and interceptor stack (for example, parse the parameters of HttpServletRequest and encapsulate them into objects and send them to the action)
(Xml code is used to parse xml documents and use dom or sax to get xml content and nodes. Then, java classes and methods are loaded using reflection technology)
7. Call the corresponding method of action after the interceptor is called to process the request (page Controller)
8. generated by struts2 for action or spring, and the struts-spring-plugin plug-in will search for and pass the serviceImpl required by the action in the spring container into the action (or an instance that provides the serviceImpl component in factory mode)
9. action calls service, and service calls dao
Call the hibernateTemplate method in the dao layer to complete database operations and return the results to the action layer by layer.
10. action
10. actionInvacation is based on struts. in xml, the view name returned by the action corresponds to the result value. Find the corresponding view and combine the view and return values (jsp obtains the value) and generate a new jsp to return the filter to the upper layer.
StrutsPrepareAndExecuteFilter returns jsp to tomcat
Tomcat jsp Engine (org. apache. jasper. servlet. JspServlet) to generate the serlvet source code (. java file)
. Then, compile the Servlet Source program into a. class file. compile it into a. class file and return it to the user to generate html

I. Struts2 generates action
Do not declare @ Component, the action generated by struts2 (prototype is a new request by default)
The set method generated by the object of the service layer to be accessed in the action is provided by the jar package of the struts-spring-plugin.jar to help us find the bean with the same name as the set Method in the spring container to help us inject,
Note that the injection method provided by struts-spring-plugin cannot be replaced, even if
Other implementations specified by @ Resource are also invalid.

II. Spring generates action
For the action generated by the Spring container, @ Component and @ Scope ("prototype") struts are configured. The class attribute of action in xml is specified
@ Component value
The set Method of the service in this action requires @ Resource injection. struts-spring-plugin will not automatically search for the corresponding bean in the spring container.

Note:
If the action does not use @ Component ("actionName"), the action is generated by struts2. If @ Component ("u") is added
If the class of <action name = "u" class = "u"> is u, it is the action generated by spring. When struts2 finds an action, it goes to the spring container to find the action.

I just summarized my work. If the errors and errors are incorrect, you are welcome to correct them.

Related Article

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.