Spring parent-child context (WebApplicationContext) (prevents transaction failure), applicationcontext

Source: Internet
Author: User

Spring parent-child context (WebApplicationContext) (prevents transaction failure), applicationcontext

If you use the listener to load configurations, the listener is usually used in Struts + Spring + Hibernate projects. As follows:

<listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>   

Spring will create a WebApplicationContext context, called the parent context (parent container) and save it in ServletContext. The key is the value of WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.

You can use the tool class provided by Spring to retrieve the context object: WebApplicationContextUtils. getWebApplicationContext (ServletContext );

 

DispatcherServlet is a Servlet that can be configured at the same time. Each DispatcherServlet has its own context object (WebApplicationContext), which is called a sub-context (sub-container). The sub-context can access the content in the parent context, however, the parent context cannot access the content in the sub-context. It is also saved in ServletContext, and the key is "org. springframework. web. servlet. FrameworkServlet. CONTEXT" + Servlet name. When a Request object is generated, the sub-CONTEXT object (WebApplicationContext) is saved in the Request object. The key is DispatcherServlet. class. getName () + ". CONTEXT ".

You can use the tool class to retrieve the context object: RequestContextUtils. getWebApplicationContext (request );

 

Note: Spring does not limit us. We must use the Parent-Child context. We can decide how to use it.

Solution 1: Traditional:

 

The Bean that stores data sources, service layers, DAO layers, and transactions in the parent context container.

Bean that saves Mvc-related actions in the sub-context container.

Transactions are controlled at the service layer.

Because the parent context container cannot access the content in the sub-context container, the transaction Bean cannot access the content in the sub-context container in the parent context container, you cannot perform AOP (transaction) on the actions in the sub-context container ).

Of course, as a "traditional" solution, there is no need to do this.

Solution 2: Aggressive:

The idea of "interface-oriented programming" in the Java World is correct, but in the system that adds, deletes, modifies, and queries the primary business, the Dao layer interface, Dao layer implementation class, and Service layer interface, service layer implementation class, Action parent class, Action. Add a large number of O (vo \ po \ bo) and jsp pages. Write a small function 7 or 8 classes. Developers say that I just want to get a private employee and compete with PHP and ASP, but I am a Java programmer. The best result is that large projects can be done well and small projects can be done quickly. Therefore, the "aggressive" solution has emerged-there is no interface, no Service layer, and there can be no large number of O (vo \ po \ bo ). Which layer does not have Service-layer transaction control? The Action layer has to be increased.

This article does not want to say whether this is the correct idea. I want to say that Spring will not limit you to do so.

With the Parent-Child context, you cannot achieve this goal. Solution:Use only the sub-context container instead of the parent context container. Therefore, the data source, service layer, DAO layer, transaction Bean, and Action Bean are all placed in the sub-context container. The transaction (annotation transaction) will work normally. This is radical enough.

Summary:Without using the listener to load the spring configuration file, we only use DispatcherServlet to load the spring configuration. Instead of the parent-child context, we only use one DispatcherServlet. This is simple, and nothing is troublesome..

 


Initializing Spring root WebApplicationContext fails to connect to the database.

Check the configuration in your dataSource to see if it can be connected to the database.
Connection timeout--, the problem is not very clear, you can paste some code to see it.

BEAN cannot be obtained in SPRING + HIBERNATE WebApplicationContext

ApplicationContextAware

You can implement this interface, and there will be a method in it. You just need to define an attribute and assign it to it, like this:
ApplicationContext ctx;
Public abstract void setApplicationContext (ApplicationContext applicationcontext)
Throws BeansException {
This. ctx = applicationcontext;
}

Then you can use ctx. getBean ("") to get it.
 

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.