3

私のSpring MVC Webアプリケーションでは、xmlベースの構成と注釈を混在させたいと考えています. 、など@Controllerの注釈を使用して、をメソッドに解決します。したがって、私は追加しました@RequestMapping("bla.htm")@RequestParamHttpRequestController

<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これらの属性は、アノテーションを介して注入できます。しかし、スコープも定義しています。したがって、属性ごとに2つの注釈があり、コードが読みにくくなります。したがって、ファイルに依存関係を挿入したいと考えていapplicationContext.xmlます。

注釈駆動型のリクエスト マッピングを維持しながら、依存関係の挿入に context.xml ファイルを使用する方法はありますか? または、注釈またはxml構成のいずれかのみを使用できますか?

注: 依存性注入の Bean は別の xml ファイルにあります。

PS:

私はSpring 2.5を使用していて、アップグレードできません。

4

2 に答える 2

4

いいえ、<mvc:annotation-driven>XML で正常に動作します。ただし、 を取り除く必要があります<context:component-scan>

更新: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>
于 2011-07-14T10:38:06.893 に答える
2

はい、これは確かに可能です。

などのコントローラー注釈を使用するには、@Controller必ず@RequestMapping配置してください

<mvc:annotation-driven/>

あなたの<servletname>-servlet.xml

次に、次のような通常の XML Bean 表記を使用してコントローラーを簡単に定義します。

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

これらの Bean 参照はapplicationContext.xml、あなたの定義された他のものからも取得できますweb.xml

于 2011-07-14T10:36:31.743 に答える