Spring MVC Learning------------Webapplicationcontext

Source: Internet
Author: User

Parent-Child Context (Webapplicationcontext)

If you use the listener listener to load the configuration, the listener listener is typically used in struts+spring+hibernate projects. As follows

Java code
  1. <listener>
  2. <listener-class>org.springframework.web.context.contextloaderlistener</listener-class >
  3. </listener>

Spring creates a webapplicationcontext context, called the parent context (parent container), which is stored in ServletContext, and key is Webapplicationcontext.root_web_ The value of the Application_context_attribute.

The context object can be fetched using the tool class provided by spring: Webapplicationcontextutils.getwebapplicationcontext (ServletContext);

Dispatcherservlet is a servlet that can be configured at the same time, with each dispatcherservlet having its own context object (Webapplicationcontext), called a Child context (child container), A child context can access content in the parent context, but the parent contexts cannot access the content in the child context. It is also stored in the ServletContext, and key is the "Org.springframework.web.servlet.FrameworkServlet.CONTEXT" +servlet name. When a request object is generated, the child context object (Webapplicationcontext) is saved in the Request object, and key is DispatcherServlet.class.getName () + ". CONTEXT ".

You can use the tool class to remove the context object: Requestcontextutils.getwebapplicationcontext (Request);

Description: Spring does not restrict us, and must use a parent-child context. We can decide for ourselves how to use it.

Scheme one, traditional type:

The parent context container holds the bean for the data source, the service layer, the DAO layer, and the transaction.

The bean that holds the MVC-related action in the child context container.

Transactions are controlled at the service level.

Because the parent context container cannot access the content in the child context container, the transaction's bean cannot access the contents of the child context container in the parent context container, and the action in the sub-context container cannot be AOP (transactional).

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

Scenario Two, Radical:

The Java world's "interface-oriented programming" idea is correct, but in the deletion and modification of the main business system, DAO layer Interface, DAO Layer implementation class, service layer interface, service layer implementation class, action parent class, action. Plus a multitude of O (vo\po\bo) and JSP pages. Write a small function 7, 8 classes are written out. Developers say I just want to contact private work, and php,asp rob a job, but I am a Java programmer. The best result is that large projects can do well and small projects can be done quickly. So the "aggressive" scenario arises-----No interface, no service layer, and no large number of O (Vo\po\bo). What layer does the service level transaction control? Had to rise the action layer.

This article does not want to say that this is not the right idea, and I would say that spring does not restrict you from doing so.

Because of the parent-child context, you will not be able to achieve this goal. The solution is to use only the child context container, not the parent context container . So the data source, the service layer, the DAO layer, the transaction bean, and the action bean are all placed in the child context container. Can be implemented, the transaction (annotation transaction) is working properly. That's enough to be radical.

Summary: Do not use the listener listener to load the spring configuration file, only use Dispatcherservlet to load the spring configuration, not the parent-child context, using only one dispatcherservlet, things are simple, There's nothing wrong with the trouble.

java--large projects can do well-in accordance with the traditional way to do, well-behaved, good expansion, good maintenance.

java--Small project can do fast-in a radical way, a week can be a version, first online to accept the market (user) feedback, and then improve, and then feedback, time is life (cost).

Reprint please indicate source: Original address: http://elf8848.iteye.com/blog/875830

Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with

Spring MVC Learning------------Webapplicationcontext

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.