這幾天一直在看李剛的《輕量級java EE》學習Spring。在看到Aop 切面時,使用註解的方法來配置。在配置文件中遇到了一點小困惑。
<!-- 指定自動搜索Bean組件、自動搜索切面類 -->
<context:component-scan base-package="com.dairui.service,com.dairui.advice" >
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!-- 啓動@AspectJ支持 -->
<aop:aspectj-autoproxy/>
<context:component-scan>這個標籤的作用就是掃描base-packge目錄下包,將含有註解的java 類註冊爲Bean組件
於是不懂爲什麼還要加上子標籤
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
於是做了各種測試與百度,明白了<context:component-scan>這個標籤 是如果掃描到有
@Component @Controller@Service等這些註解的類,則把這些類註冊爲bean。是不掃描@Aspect註解的。
所以我們需要在子標籤添加 <context:include-filter type="annotation"expression="org.aspectj.lang.annotation.Aspect"/>
Use-dafault-filters=”false”的情況下:<context:exclude-filter>指定的不掃描,<context:include-filter>指定的掃描
隨便貼上博主關於<context:component-scan> 詳細介紹:http://blog.csdn.net/suyu_yuan/article/details/52635940
附上完整的bean.xml:
<?xml version="1.0" encoding="GBK"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 指定自動搜索Bean組件、自動搜索切面類 -->
<context:component-scan base-package="com.dairui.service,com.dairui.advice" >
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!-- 啓動@AspectJ支持 -->
<aop:aspectj-autoproxy/>
</beans>