2014-05-20 35 views
0

我創建了一個基於教程的彈簧應用程序,並且有些東西對我來說完全不清楚,我希望有人能夠清除它。註解的彈性配置

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

如果我想要在控制器和我的bean(daos和services)中使用註釋,是否需要在兩個xml文件中包含以下內容或這些東西是否被繼承?

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

回答

1

會發生什麼事,當你設置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> 

應用程序配置.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:annotation-config/>是不需要的,因爲<context:component-scan>提供了功能的超集

+0

感謝您的答案,這有助於很多,我現在要改變我的配置到你的建議,你能回答最後一個問題嗎?是僅在控制器(mvc)配置中需要mvc:annotation驅動標籤,還是我的bean配置中需要的標籤?謝謝 – berimbolo

+0

歡迎您! 'mvc:annotation-driven />'只能出現在'mvc-config.xml'中 – geoand