【问题标题】:Spring MVC 2.5 Using a mix of annotations & XML configuration but xml gets ignoredSpring MVC 2.5 混合使用注释和 XML 配置,但 xml 被忽略
【发布时间】:2011-10-05 04:55:38
【问题描述】:

在我的 Spring MVC web 应用程序中,我想将基于 xml 的配置与注释混合: 我使用@Controller@RequestMapping("bla.htm")@RequestParam 等注释来将HttpRequests 解析为Controller 方法。所以我加了

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:component-scan base-package="somePackage.controller"/>

致我的dispatcher-servlet.xml

但是我的控制器有属性。这些属性可以通过@AutoWired 注解注入。但我也确实定义了范围。所以我每个属性都有两个注释,这使得代码可读性差。所以我想在我的applicationContext.xml 文件中注入依赖项。

有没有一种方法可以保留注释驱动的请求映射,但使用 context.xml 文件进行依赖注入?还是只能使用注解或 xml 配置?

注意:我用于依赖注入的 bean 位于不同的 xml 文件中。

PS:

我应该提到,我使用的是 Spring 2.5,无法升级。

【问题讨论】:

    标签: spring spring-mvc dependency-injection annotations applicationcontext


    【解决方案1】:

    不,&lt;mvc:annotation-driven&gt; 适用于 XML。但是你需要去掉&lt;context:component-scan&gt;

    更新:使用 Spring 2.5,这应该可以帮助您入门:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:annotation-config />
    
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
        <!-- now add some controllers -->
    
    </beans>
    

    【讨论】:

    • @mosk 它应该在 2.5 中工作,但“它不起作用”并没有真正的帮助。请根据您现在使用的上下文更新问题
    • 我编辑了我的开始帖子。使用 会导致错误消息,发布在我的 start-post 中。
    • @mosk 哎呀,对不起,我错了。该功能存在于 2.5 中,但 mvc 命名空间不存在。如果可以的话,升级到 3.x
    【解决方案2】:

    是的,这当然是可能的。

    要使用 @Controller@RequestMapping 等控制器注解,请确保您放了

    <mvc:annotation-driven/>
    

    在你的&lt;servletname&gt;-servlet.xml

    然后使用普通的 XML bean 表示法简单地定义您的控制器,例如:

    <bean class="com.company.controllers.AController">
        <property name="propertyName" ref="beanId" />
    </bean>
    

    这些 bean 引用也可以来自您的 web.xml 中定义的任何其他 applicationContext.xml

    【讨论】:

    • 啊,是的,你需要 Spring 3.0。不过不用担心,这意味着您不能使用 &lt;mvc:annotation-driven/&gt; 元素。只需手动声明您的DefaultAnnotationHandlerMapping 和您的AnnotationMethodHandlerAdapter,它应该可以工作。
    • 但是我已经声明了这两个 bean。请参阅我的开始帖子。还是声明中遗漏了什么?
    • 是的,对不起,我应该更具体。 &lt;mvc:annotation-driven&gt; 元素会自动为您声明这两个 bean。由于您使用的是 Spring 2.5,因此无论如何您都不能使用 &lt;mvc:annotation-driven&gt; 快捷方式,因此您必须自己在 context.xml 中声明它们。只需保留上面提到的两个 bean 定义,一切都会正常。
    猜你喜欢
    • 2011-02-19
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2015-07-11
    • 2016-04-01
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多