spring aop 配置文件 xml文件

时间:2021-09-29 20:33:03
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<!-- 前置通知(方法前通知) -->
<bean id="myMethodBeforeAdvice" class="cn.itcast.spring.aop.advice.MyMethodBeforeAdvice" />
<!-- 后置通知(方法返回后通知) -->
<bean id="myAfterReturningAdvice" class="cn.itcast.spring.aop.advice.MyAfterReturningAdvice" />
<!-- 环绕通知(方法拦截器) -->
<bean id="myMethodInterceptor" class="cn.itcast.spring.aop.advice.MyMethodInterceptor" />
<!-- 环抛出异常知-->
<bean id="myThrowsAdvice" class="cn.itcast.spring.aop.advice.MyThrowsAdvice" />
<!-- 引入通知(代理引入拦截器) -->
<bean id="myDII" class="cn.itcast.spring.aop.advice.MyDII" />


<!-- 名称匹配方法切入点通知(对原来的通知的包装,增加定义切入点功能) -->
<bean id="beforeAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myMethodBeforeAdvice" />
<property name="mappedNames">
<list>
<value>sayName</value>
</list>
</property>
</bean>
<!-- 后置通知包装 -->
<bean id="afterAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myAfterReturningAdvice" />
<property name="mappedNames">
<list>
<value>sayName2</value>
</list>
</property>
</bean>
<!-- 默认的引入切入点通知(类似于名称匹配方法切入点通知) -->
<bean id="defaultIntroductionAdvisor"
class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="myDII" />
</bean>


<!-- 目标对象 -->
<bean id="welcomeServiceTarget" class="cn.itcast.spring.aop.service.WelcomeServiceImpl">
<property name="name" value="tom" />
</bean>

<!-- 代理对象
<bean id="welcomeService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>cn.itcast.spring.aop.service.WelcomeService</value>
<value>cn.itcast.spring.aop.service.WelcomeService2</value>
<value>cn.itcast.spring.aop.service.ModifyDate</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvisor</value>
<value>afterAdvisor</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
<value>defaultIntroductionAdvisor</value>
</list>
</property>
<property name="target" ref="welcomeServiceTarget" />
<property name="proxyTargetClass" value="true" />
</bean>
-->
<!-- bean名自动代理创建器,按照bean的名称自动创建代理对象
<bean id="beanNameAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>beforeAdvisor</value>
<value>afterAdvisor</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
<value>defaultIntroductionAdvisor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>*ServiceTarget</value>
</list>
</property>
</bean>
-->

<!-- 默认的切入点通知自动代理创建器(将所有的advisor应用到所有的业务bean上去,能否加通
知,要看是否满足切入点的要求) -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>