Spring MVC Annotations with Global Context context:component-scan?

后端 未结 1 1588
野的像风
野的像风 2021-01-06 17:50

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



        
相关标签:
1条回答
  • 2021-01-06 18:30

    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>
    
    0 讨论(0)
提交回复
热议问题