3

I have a spring dispatcher servlet with servlet-name "spring-mvc". The spring-mvc-servlet.xml appears as follows:

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" 
        value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

In a file in WEB-INF/annotation-context.xml I have the annotation scanner defined. All of my annotated classes are loaded and other spring beans are able to load them ok.

However, the path mappings do not work from spring-mvc. If I copy the context-scanner to spring-mvc-servlet.xml, then they work.

Is it possible for spring-mvc-servlet.xml to reference beans defined at the global spring level?

2
  • there's no such thing as "global spring level". What does this WEB-INF/annotation-context.xml file do, and what refers to it? Commented Nov 18, 2009 at 16:27
  • global spring level == application context (as opposed to a nested context in which a dispatcher servlet is defined) Commented Nov 23, 2009 at 18:43

1 Answer 1

2

You can load your contexts hierarchically so that context described in annotation-context.xml becomes a parent of your Spring MVC context. The latter will then be able to access all the beans defines in the former.

Spring documenation describes several ways to do it. For example, in your web.xml:

// load parent context
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/annotation-context.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

// load Spring MVC context
<servlet>
  <servlet-name>spring-mvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thats exactly what I'm doing. Controller's in the parent context are not seen by spring mvc in spring-mvc servlet's context. They can be called manually however, just that dispatcherservlet doesn't see them or their path mappings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.