The difference between Dispatcherservlet and Contextloaderlistener in spring

Source: Internet
Author: User


What is the difference between Dispatcherservlet and Contextloaderlistener in spring?


1.[stackoverflow's question 1] (Http://stackoverflow.com/questions/9016122/contextloaderlistener-or-not)
>a Standard Spring Web application (created by Roo or "Spring MVC Project" Template) create A web.xml with ' contextload Erlistener ' and ' Dispatcherservlet '. **why do they the ' dispatcherservlet ' and make it to load the complete configuration?**
>
>i understand that the contextloaderlistener should being used to load the stuff this is not web relevant and the Dispatch Erservlet is used to load the Web relevant stuff (controllers,...). And this is the two contexts:a parent and a child context.
>
Background:
>
>i was doing it the standard way for several years.
>
> ""
><context-param>
> <param-name>contextConfigLocation</param-name>
> <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
></context-param>
>
><!--creates the Spring Container shared by all servlets and Filters-->
><listener>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
></listener>
>
><!--Handles Spring requests-->
><servlet>
> <servlet-name>roo</servlet-name>
> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
> <init-param>
> <param-name>contextConfigLocation</param-name>
> <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
></servlet>
> ""
>
>this often caused problems with the two contexts and the dependencies between. In the past I is >always able to find a solution, and I have the strong feeling, which this makes the software >struc Ture/architecture always better. But now I am facing a [problem with the events of the both >contexts]> (http://stackoverflow.com/questions/8534222/ho W-to-bridge-spring-application-context-events-to->an-other-context).


>--However This makes me rethink this two context pattern, and I am asking myself:why I should bring This is trouble, why not loading all spring configuration files with one ' Dispatcherservlet ' and >removing the ' Contextloa Derlistener ' completely. (I still'll to have different configuration files, but >only one context.)
>
>is there any reason "not" to remove the ' Contextloaderlistener '?


Translation is:
A standard spring Web project (created by the Spring MVC project template or roo) will include ' Contextloaderlistener ' and ' Dispathcerservlet ' in the web.xml. * * Why do some can load all configurations by simply referencing ' Dispatcherservlet '? * *
I know that contextloaderlistener should be used to load web-independent beans, dispatcherservlet to load web-related beans (controller related, and so on). This results in two context: a parent and a self.


Background:
I have been doing it for many years in accordance with standard practice.
Code PART * * *
This often causes a dependency problem between the two context. In the past I can always find a corresponding solution, and I strongly believe that this mechanism can make the software architecture better. But I now face a problem [problem with the events of the both >contexts] (http://stackoverflow.com/questions/8534222/ How-to-bridge-spring-application-context-events-to->an-other-context).


But this question also makes me rethink the two-context model, and I ask myself: Why do I have to bring myself into this trouble, Why not put all of spring's configuration in one Dispatcherservlet file and get out of Contextloaderlistener. (I will also have different profiles, but only one context).
Is there any reason to support the removal of ' contextloaderlistener '?


(Here is the answer)
>in your case, no, there ' s no reason to keep the Contextloaderlistener and Applicationcontext.xml. If your app fine with just the servlet's context, that stick with that, it ' s simpler.
>
>yes, the generally-encouraged pattern are to keep non-web stuff in the webapp-level context, but it's nothing more than A weak convention.
>
>the only compelling reasons the Webapp-level context are:
>
>if you have multiple dispatcherservlet this need to share services
>if you have legacy/non-spring servlets which need access to spring-wired services
>if your have servlet filters that hook into the webbapp-level context (e.g. Spring security ' s Delegatingfilterproxy, Op Enentitymanagerinviewfilter, etc)
>none the extra complexity is unwarranted.
>
>just be careful as adding background tasks to the servlet's context, like scheduled tasks, JMS connections, etc. If you are forget to add <load-on-startup> to your web.xml, then this tasks won ' t be started until the ' The servlet.


Translation is:
In your case, there is no reason to use the Contextloaderlistener and Applicationcontext.xml configuration files. If your app works well with only dispatcherservlet, you should stick with it, this is simple.
Yes, the general recommendation is to put the web irrelevant to the context of the webapp level, but it's a bit inconvenient except for nothing.
The reasons for the need for strong use of WebApp are as follows:
* If you have multiple dispatcherservlet that require shared services
* If you have a logical connection, you need access to the service within spring
* If you have a servlet filters that you need to hook at the webapp level.


If none of this is right for you, you don't have to add complexity at all.
One thing to be concerned about is that when adding background tasks to the context of the servlet, such as scheduling tasks, JMS connections, and so on, if you forget to add <load-on-startup&gt, these tasks will not be the first.


[Problem 2:difference between Applicationcontext.xml and Spring-servlet.xml in spring Framework] (http://stackoverflow.com/questions/3652090/ Difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring-frame)
>are Applicationcontext.xml and Spring-servlet.xml related anyhow in spring Framework?
Would the properties files declared in Applicationcontext.xml is available to Dispatcherservlet?
On a related note, why doing I need a *-servlet.xml at all? Why is applicationcontext.xml alone insufficient?


(Below is the answer:)


>spring lets you define multiple contexts in a parent-child hierarchy.
>
>the Applicationcontext.xml defines the beans for the ' root webapp context ', i.e. the context associated with the WEBAP P.
>
>the Spring-servlet.xml (or whatever else it) defines the beans for one servlet "s app context. There can be many to WebApp, one per Spring servlet (e.g. Spring1-servlet.xml for servlet spring1, Spring2-serv Let.xml for servlet spring2).
>
>beans in Spring-servlet.xml can reference Beans into Applicationcontext.xml, but not vice versa.
>
>all Spring MVC controllers must go in the spring-servlet.xml context.
>
>in Most simple cases, the applicationcontext.xml context is unnecessary. It is generally used to contain beans this are shared between all servlets in a webapp. If you are only have one servlet, then there's not really much of the point, unless you have a specific to use for 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.