具有全局上下文上下文的Spring MVC注释:组件扫描? [英] Spring MVC Annotations with Global Context context:component-scan?

查看:137
本文介绍了具有全局上下文上下文的Spring MVC注释:组件扫描?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有servlet-namespring-mvc的spring dispatcher servlet。 spring-mvc-servlet.xml如下所示:

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

在WEB-INF / annotation-context.xml中的文件中,我定义了注释扫描程序。我加载了所有带注释的类,其他spring bean可以加载它们。

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.

但是,路径映射不能从spring-mvc开始工作。如果我将context-scanner复制到spring-mvc-servlet.xml,那么它们就可以工作。

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

spring-mvc-servlet.xml是否可以引用定义的bean在全球春季水平?

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

推荐答案

你可以按层次结构加载上下文,以便在 annotation-context.xml中描述上下文成为Spring MVC上下文的父级。后者将能够访问前者中定义的所有bean。

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文档描述了几种方法。例如,在 web.xml 中:

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>

这篇关于具有全局上下文上下文的Spring MVC注释:组件扫描?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆