Spring无法在servlet-context和contextConfigLocation bean之间看到bean
我有一个spring mvc项目设置如下:
appServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring-contexts/servlet-context.xml 1
contextConfigLocation /WEB-INF/spring-contexts/configuration-context.xml
如果我在configuration-context.xml中创建一个bean并在servlet-context.xml中引用一个bean,它就无法找到它。 这些是作为两个不同的背景创建的吗? 为什么这种情况一般会发生/这样?
是的,有两个上下文相互堆叠(父和子上下文)。
DispatcherServlet( servlet-context.xml
)中的bean可以从ContextLoaderListener( configuration-context.xml
)访问bean,但不能configuration-context.xml
。
因此,将基本内容放在configuration-context.xml
,将web相关内容放入servlet-context.xml
。
@参见Stack Overflow这个问题: ContextLoaderListener与否?