The relationship between spring container and Springmvc container, and Web container

Source: Internet
Author: User

When it comes to spring and SPRINGMVC, there are a lot of people who have been working for years and can't tell you what the difference is, if you ask him what MVC technology he uses, he'll say we use spring and mybatis, or spring and hibernate.

In the subconscious mind will think that SPRINGMVC is spring, and I think so, haha.

Although the SPRINGMVC and spring have a certain connection, but they also have the difference. I'll briefly describe the following

First of all, SPRINGMVC and spring are both containers, where it's easy to manage objects, such as Tomcat, which manages the servlet objects, and the Springmvc container and the spring container, which is where the Bean object is managed, and the straightforward point,

SPRINGMVC is a container for managing controller objects, and spring is the container for managing service and DAO, you see. So the scan path that we configured in the SPRINGMVC configuration file is the controller's path, and the spring configuration file naturally comes with the service and DAO paths .

Spring-mvc.xml

<context:component-scan base-package = "Com.smart.controller"/>

Applicationcontext-service.xml

<!--scan Packages load Service implementation class--    <context:component-scanbase-package = "Com.smart.service" ></ Context:component-scan>
Or
<context:component-scan base-package= "Com.smart" >
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>
</context:component-scan>

  As to how he managed it, and how it was injected into the property, it involved their underlying implementation technology.
Secondly, the relationship between the spring container and the SPRINGMVC is the parent-child container. The spring container is the parent container, and SPRINGMVC is a child container. Objects in the parent container can be accessed in the child container, but the child container's objects cannot be accessed in the parent container.
The popular point is that you can access the service object in the controller, but you cannot access the Controller object in the service.
So if so, all the beans are managed by spring or springmvc containers and they can be injected directly. The Springmvc Interceptor is then managed by the SPRINGMVC container , so the bean object can be injected directly into the SPRINGMVC interceptor.

<mvc:interceptors>        <mvc:interceptor>            <mvc:mapping path= "/employee/**" ></MVC: Mapping>            Class= "Com.smart.core.shiro.LoginInterceptor" ></bean>        </MVC: Interceptor>    </mvc:interceptors>


  
And what the hell is a Web container?
The Web container is the management servlet, as well as the Listener (Listener) and filter (filters). these are within the control of the Web container. But they are not in the control of spring and Springmvc . Therefore, it is not possible to use the spring annotations directly in these classes to inject the objects we need, which is not valid,
The Web container is not recognized.

But sometimes we do have the need to do some validation or initialization when the container is started, perhaps by using a bean object in the listener, or by defining a filter to do some interception, or a bean object.
So how do you get spring's bean objects in these places? Below I provide two methods:

1.

 Public void  == (userservice) context.getbean ("UserService");}

2.

 Public void  == (userservice) webapplicationcontext.getbean ("UserService");}

  Note : The above code has the premise that the servlet container must be initialized before instantiating the Configlistener and calling its methods ! The spring container initialization is also done by listener (Contextloaderlistener), so just configure the listener of the spring container first in Web. XML, and then configure your own listener.

The relationship between spring container and Springmvc container, and Web container

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.