天天看點

spring使用注解時配置檔案的寫法

在spring的配置檔案中:

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

       xsi:schemalocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <context:annotation-config/> 

</beans>

我們會看到這樣寫:

    <context:component-scan base-package="cn.test">

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/>

    </context:component-scan>

<context:component-scan/> 配置項不但啟用了對類包進行掃描以實施注釋驅動 bean 定義的功能,同時還啟用了注釋驅動自動注入的功能(即還隐式地在内部注冊了 autowiredannotationbeanpostprocessor 和 commonannotationbeanpostprocessor),是以當使用 <context:component-scan/> 後,就可以将 <context:annotation-config/>

移除了。

<context:component-scan/> 的 base-package 屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。

通過exclude-filter 把所有@controller注解的控制器元件排除。因為我們知道@controller一般标注的是action,不用把action注入的什麼地方。

參考博文:

http://913.iteye.com/blog/1280808