Spring MVC annotations with global context: component scanning?

I have a spring dispatcher servlet with the servlet name "spring -mvc". spring -mvc-servlet.xml is 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 the file in WEB-INF / annotation-context.xml, I have an annotation scanner installed. All my annotated classes are loading, and other spring beans can load them in order.

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

Is it possible for spring -mvc-servlet.xml to refer to beans defined globally by spring?

+3
source share
1 answer

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

Spring documenation describes several ways to do this. For example, in 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> 
+2
source

Source: https://habr.com/ru/post/898837/


All Articles