Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring can't see beans between servlet-context and contextConfigLocation beans

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>
</servlet>

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-contexts/configuration-context.xml</param-value>
</context-param>

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?

like image 340
mogronalol Avatar asked Mar 02 '12 10:03

mogronalol


1 Answers

Yes there are two contexts stacked on each other (parent and child context).

The beans from the DispatcherServlet (servlet-context.xml) can access the beans from the ContextLoaderListener (configuration-context.xml), but not the other way around.

So put the basic stuff in the configuration-context.xml and the web related once into servlet-context.xml.

@See also this Stack Overflow question: ContextLoaderListener or not?

like image 180
Ralph Avatar answered Nov 12 '22 09:11

Ralph