簡體   English   中英

注釋的Spring配置

[英]Spring configuration for annotations

我已經基於教程創建了一個spring應用程序,有些事情對我來說還不是很清楚,我希望有人可以解決。

我有2個配置文件-mvc-config.xml用於調度程序servlet,而application-config.xml用於構成應用程序的bean。

如果我想在控制器和我的bean(dao和服務)中都使用批注,是否需要在xml文件中都包含以下內容,或者這些東西是否繼承?

<annotation-driven />
<context:component-scan base-package="com.ws.jaxb" />
<context:annotation-config />    

將Spring設置為同時使用mvc-config.xmlapplication-config.xml會發生兩個創建應用程序上下文的情況。 根上下文(與application-config.xml對應)和Web上下文(與mvc-config.xml對應)。

在您的情況下,您需要執行以下操作以使所有功能按預期工作:

MVC-config.xml中

<mvc:annotation-driven /> <!-- for standard Spring MVC features -->
<context:component-scan base-package="com.ws.jaxb" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

應用-config.xml中

<context:component-scan base-package="com.ws.jaxb">
   <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

上面提到的代碼的作用是僅將控制器添加到Web上下文,而其余所有Spring Bean都添加到根上下文。 另請注意,由於<context:component-scan>提供了功能的超集,因此不需要<context:annotation-config/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM