Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

java - Use a ContextLoaderListener in accordance with DispatchServlet

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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For both ContextLoaderListener and DispatcherServlet, the contextConfigLocation parameter is optional.

ContextLoaderListener defaults to /WEB-INF/application.xml, DispatcherServlet defaults to /WEB-INF/servletname-servlet.xml.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...