Parent-child relationship between the container and the spring of springMVC

BACKGROUND AND SUMMARY

In the spring and springMVC bean objects can be managed through the IOC, there are two profiles can be configured ioc

spring configuration file applicationContext.xml
profile springMVC.xml springMVC of
work we use spring to manage the bean object service layer and layer of repertory, and let springMVC to manage controller.
  
Then it naturally follows question?

1, can manage all of the bean object configuration file springMVC.xml springMVC, including controller, service, dao?
2, can not manage all of the bean object configuration file applicationContext.xml spring, including the controller, service, dao?
The answer is this

  SpingMVC can be used in the configuration file and to manage all of Bean object, but can not use spring to manage the controller.

Why is this?

Spring and SpringMVC parent-child relationship containers

  In the overall framework of the core concepts Spring, the container is the core idea, Bean is used to manage the entire life cycle, and in a project, not necessarily only a container, Spring may include a plurality of containers, and the container has upper and lower relations, the most common scenario is the introduction of these two frameworks Spring and SpringMVC in a project, it is actually two containers, Spring is the parent container, SpringMVC is its children, and registered in the Spring parent container for SpringMVC Bean container is visible, and registered in SpringMVC container for Spring Bean parent container is not visible, that is, the child can see the parent container vessel registered Bean, on the contrary can not.

Details of spring and springMVC

   spring is an IOC container, springMVC can be seen as a sub-container springIOC a container, the child container has its own unique logic and methods. One very important which are: HandlerMapper (Processor mapper), and HandlerAdapter is (Processor Adapter), which is configured as follows:

<! - Configure the latest version of the annotation processor mapper ->
<bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> </ bean>
<! - Configure the latest version the adapter processor annotation ->
<the bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> </ the bean>
. 1
2
. 3
. 4
  , or annotations may be utilized to drive automatically loaded.

<! - annotation drive:
the role: for us automatically configure the latest version of the mapping and annotation processor handler adapter
->
<MVC: Annotation-Driven> </ MVC: Annotation-Driven>
. 1
2
. 3
. 4
  noteworthy that, when calling HandlerMapper be springMVC url to controller function mapping analytical method, HandlerMapper will look at springMVC container controller, which is looking for the child container, do not go looking for parent container spring container, so when a spring after applicationContext.xml configure the controller, 404 errors occur when accessing the page.

Let's summarize what
  we know in the spring, and parent-child relationship springMVC container, as well as the principle of scanning registered after, according to official suggested that we could very well put different types of Bean assigned to different container management. Then there is a problem Bean can not find or can not jump as well as the configuration SpringMVC transaction failures

 

https://blog.csdn.net/u010758410/article/details/79950801

 

He said the father and son in a container before, we first understand what is container:

java container:

    Objects can be managed lifecycle, dependencies between objects. Can also configure the object name, attributes, generation methods. Programmers do not have to write your own program to manage.

java class of commonly used containers: List, HashMap, HashTable like.

 

spring container:

Borrow that sum up blogger:

    spring has two core interfaces: BeanFactory and ApplicationContext, which ApplicationContext is a subinterface of BeanFactory. They can be representatives of Spring containers, Spring containers are generated Bean instance factory, and manage the container Bean.

    Bean is the basic unit of management Spring, Spring-based Java EE application, all of the components are processed as Bean, includes a data source, hibernate of the SessionFactory, the transaction manager and the like. In Spring, Bean is a very broad concept, any Java objects, Java components are treated as a Bean.

 

Understand what is container, then get to the point:

    ssm framework of a project, the container is not necessarily the only one who took me are learning: spring may include a plurality of containers, such as springmvc container. When building the project we have done a lot of configuration:

For example: in a configuration springmvc.xml scan package, the scanning controller annotation. (Behind mvc: annotation-driven representatives to use annotation-driven, you can not write)

 

Another example: in service.xml configure the scan packages to scan annotation service in

 

 

     That question came, since spring container is a large container that can I write a package to the global scan service / dao / controller are scanned into the spring container it? This will not be a little more convenient configuration without so much xml file?

 

 

To address this issue, we work together to learn about:

     Spring is a container, springmvc also a container. Parent-child relationship between them the two containers. springmvc sub container, comprising a container in the spring. Configuration in the project are as follows:

 

We configured in web.xml a spring container: Loading the initial srping container Listener (which is a large container)

 

Also equipped with a front end controller :( springmvc of a container which is a springmvc contains. Springmvc the spring is contained within the vessel)

 

 

It is equivalent to: service.dao in the spring container, controller in springmvc vessel.

 

Sons container conventions are as follows:

1, the sub-tank can access the parent object containers:

    That is service can be injected into the controller.

    But then, controller can not be injected into the service.

    Of course, if the service controller in a vessel and, to be injected.

 

2, parent vessel may not access the sub-container objects:

    If we configure a global scan package in spirng in, spring container will service, controller, dao are scanned into the spring of this large vessel. This leads, springmvc would be no objects, the page can not find the corresponding access controller, it will report a 404 error.

    On the contrary, if we write in springmvc a scan package, the service, dao are injected into springmvc in (that is, the spingmvc.xml scan package .controller removed) is no problem. Because springmvc also a spring container.

 

3, why should we add spring springmvc it?

     Of course, in order to facilitate future expansion of the system. spring can integrate multiple frames, so if we want to join a struts back to the spring inside the container, is also possible.

 

4, can we put the transaction into the controller configuration in?

     If spirng + springmvc this framework is not possible, because the transaction manager is configured in the spring, when the parent container is not accessible springmvc sub-controller of container. So not.

     But if the package is configured to scan all springmvc, then the controller is configurable transaction.

 

Therefore, the following conclusions: a global scan package not be configured in the service.xml used.

 

     Learn the container father and son, when we build the framework, it would be easy to know, what I want to configure, to configure what areas. It will be deeper understanding of the principles of the framework.

 

Extracurricular expand:

      When I was online searching for his son vessel, they found the use of the two factions configuration container.

      The first camp: friends called traditionalists, is spring + springmvc configuration. And I say the same as above. nothing to say

      The second school: called radical, that is, all things are configured to springmvc, do not service or interface dao layer, to make the project a full range of data sources la, la affairs, dao it, service it, are configured to child containers in.

 

https://blog.csdn.net/lishaoran369/article/details/57073715

 

Guess you like

Origin www.cnblogs.com/YuyuanNo1/p/11671332.html