The relationship between DispatcherServlet and ContextLoaderListener, which one is used?

from: http://www.cnblogs.com/caryfang/p/5675178.html

Let's first look at the configuration methods of these two things:




For the contextConfigLocation parameter, there are 2 places to configure:
1) context-param is global
2) The init-param under the servlet is a local configuration.
If the same bean configuration file path is set in the above two places, the beans configured in the file will be initialized twice, so only one configuration file can be selected. There are 2 ways

to use the spring framework in the project:
1) ContextLoaderListener under the listener is an introduction method, which reads /WEB-INF/applicationContext.xml by default
2) If it is a spring-web project, DispatcherServlet is also an introduction method By default, /WEB-INF/${servlet-name}-servlet.xml is read.
If the above two configurations are introduced, the global bean configuration file will be loaded twice; and the respective configuration files of the two methods In a sense, the configuration items in it are not merged and complementary, but each is a system (although ordinary beans seem to be loaded into the global context, there are still some special beans and configuration items that are not as expected work); for
example, the AOP declarative configuration in the /WEB-INF/applicationContext.xml file:
<!--aop behavior-->
<bean id="himvn" class="com.tangbao.hellomvn.Himvn" />


















The result is as we expected: multiple bean configuration files will not be loaded repeatedly, and all aop configurations will take effect.



The above arguments are obtained from the personal test under spring 4.3.1. Of course, the essence is guessed from the performance. I haven't really studied the source code of Spring, so if there is a mistake in the principle, I hope you can point it out!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679441&siteId=291194637