Why DispatcherServlet creates another application context?

后端 未结 2 1718
囚心锁ツ
囚心锁ツ 2020-11-27 13:12

I have configured the root application context using ContextLoaderListener and the context init-parameter contextConfigLocation.

The root c

相关标签:
2条回答
  • 2020-11-27 13:50

    If you have a DispatcherServlet running, there is no need to use ContextLoaderListener. Just use ContextLoader.getCurrentWebApplicationContext() to access the WebApplicationContext.

    Just keep the bean definitions separate as outlined in this previous answer.

    0 讨论(0)
  • 2020-11-27 13:53

    To answer your first question, the DispatcherServlet creates a context because that's how it allows itself to be configured, and if you have multiple DispatcherServlets in one app, they'd each need to be configured separately. Therefore each one gets its own context, and each of those contexts is separate from the "root" context, where all of your real, "working" beans should live so they can be shared between the other contexts. There have been a number of questions over the last couple of weeks that were spawned by confusion over this very issue. You might gain a better understanding of how things work by checking out the answers:

    Spring XML file configuration hierarchy help/explanation

    Declaring Spring Bean in Parent Context vs Child Context

    Spring-MVC: What are a "context" and "namespace"?

    0 讨论(0)
提交回复
热议问题