Spring无法在servlet-context和contextConfigLocation bean之间看到bean [英] Spring can't see beans between servlet-context and contextConfigLocation beans
问题描述
我有一个spring mvc项目设置如下:
I have a spring mvc project set up like so:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/configuration-context.xml</param-value>
</context-param>
如果我在configuration-context.xml中创建bean并在servlet-context中引用bean,则会显示.xml它无法找到它。这些是作为两个不同的背景创建的吗?为什么这种情况一般会发生/这样工作?
It appears if I make a bean in configuration-context.xml and reference a bean in servlet-context.xml it cannot find it. Are these created as two different contexts? Why does this happen / work like this in general?
推荐答案
是的,有两个上下文相互堆叠(父和子上下文) )。
Yes there are two contexts stacked on each other (parent and child context).
DispatcherServlet中的bean( servlet-context.xml
)可以从ContextLoaderListener访问bean( configuration-context.xml
),但不是相反。
The beans from the DispatcherServlet (servlet-context.xml
) can access the beans from the ContextLoaderListener (configuration-context.xml
), but not the other way around.
所以把基本内容放在 configuration-context.xml
和网址相关的一次进入 servlet-context.xml
。
So put the basic stuff in the configuration-context.xml
and the web related once into servlet-context.xml
.
@参见Stack Overflow问题: ContextLoaderListener与否?
@See also this Stack Overflow question: ContextLoaderListener or not?
这篇关于Spring无法在servlet-context和contextConfigLocation bean之间看到bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!