问题描述
我想使用ContextLoaderListener(以便我可以将Spring Beans传递给我的servlet)以及DispatchServlet(Spring MVC)。但是,目前我必须将init参数传递给这两个类初始值设定项:
I want to use both ContextLoaderListener (so that I can pass Spring Beans to my servlet) as well as DispatchServlet (Spring MVC). However, currently I have to pass init param to these both class initializer:
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml
</param-value>
因此,我对这两个类使用相同的xml。不知道是否会导致我的bean被初始化两次?如果是,我该如何避免?
So, I use the same xml for these both classes. Wonder if it would lead to my beans being initialized twice? If yes, how would I do to avoid that?
推荐答案
对于 ContextLoaderListener
和 DispatcherServlet
, contextConfigLocation
参数是可选的。
For both ContextLoaderListener
and DispatcherServlet
, the contextConfigLocation
parameter is optional.
ContextLoaderListener
默认为 /WEB-INF/application.xml
, DispatcherServlet
默认为 /WEB-INF/servletname-servlet.xml
。
如果设置这些参数显式地,您应该不将它们设置为相同的值。 ContextLoaderListener
和 DispatcherServlet
应该有不同的bean定义集的上下文,否则,正如你所说,bean将会实例化两次。
If you set these parameters explicitly, you should not set them to the same value. The ContextLoaderListener
and DispatcherServlet
should have contexts with different sets of bean definitions, since otherwise, as you say, the beans will be instantiated twice.
这篇关于根据DispatchServlet使用ContextLoaderListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!