本文参考实验楼教程:https://www.shiyanlou.com/courses/578/learning/?id=1940
在上一篇文章中Spring-Aop:一、四种Advice(本文沿用上一篇文章的实验内容),我们必须为每一个需要AOP的bean创建一个Proxy(比如:customerService 与 customerServiceProxy)。
这不是一个很好的体验,例如我们想为DAO层的所有bean都支持AOP,以便写SQL日志,那么必须手工在xml配置文件中一个一个的为他们创建ProxyFactoryBean,这样会直接导致xml文件身份复杂并且臃肿,不利于维护。
Spring中,提供了两种自动创建代理的方法:
- 利用BeanNameAutoProxyCreator自动创建Proxy
- 利用DefaultAdvisorAutoProxyCreator自动创建Proxy
1、手动创建ProxyFactoryBean实例
配置好Advisor之后,手动创建ProxyFactoryBean
并将其interceptorNames
配置上Advisor。
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="customerService" class="com.shiyanlou.spring.aop.advice.CustomerService">
<property name="name" value="weihouye"/>
<property name="url" value="www.weihouyeaiyangzhan.com"/>
</bean>
<!--<bean id="weiBeforeMethod" class="com.shiyanlou.spring.aop.advice.WeiBeforeMethod"/>-->
<!--<bean id="weiAfterMethod" class="com.shiyanlou.spring.aop.advice.WeiAfterMethod"/>-->
<!--<bean id="weiThrowExceptionMethod" class="com.shiyanlou.spring.aop.advice.WeiThrowExceptionMethod"/>-->
<bean id="weiAroundMethod" class="com.shiyanlou.spring.aop.advice.WeiAroundMethod"/>
<!--<bean id="customerPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">-->
<!--<property name="mappedName" value="printName"/>-->
<!--</bean>-->
<!--<bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">-->
<!--<property name="pointcut" ref="customerPointcut"/>-->
<!--<property name="advice" ref="weiAroundMethod"/>-->
<!--</bean>-->
<!--pointcut和advisor可以一起配置,只要在配置customerAdvisor时,选择NameMatchMethodPointcutAdvisor-->
<!--<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">-->
<!--<property name="mappedName" value="printName"/>-->
<!--<property name="advice" ref="weiAroundMethod"/>-->
<!--</bean>-->
<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*URL.*</value>
</list>
</property>
<property name="advice" ref="weiAroundMethod"/>
</bean>
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService"/>
<property name="interceptorNames">
<list>
<!--<value>weiBeforeMethod</value>-->
<!--<value>weiAfterMethod</value>-->
<!--<value>weiThrowExceptionMethod</value>-->
<value>customerAdvisor</value>
</list>
</property>
</bean>
</beans>
配置完xml文件,还要在App.java
中得到customerServiceProxy
,代码如下:
context = new ClassPathXmlApplicationContext("SpringAopAdvice.xml");
CustomerService cust = (CustomerService) context.getBean("customerServiceProxy");
2、利用BeanNameAutoProxyCreator自动创建Proxy
在这种方法中,我们需要创建BeanNameAutoProxyCreator
,将所有的Bean通过正则或者名字匹配与Advisor关联,SpringAopAdvice.xml
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="customerService" class="com.shiyanlou.spring.aop.advice.CustomerService">
<property name="name" value="weihouye"/>
<property name="url" value="www.weihouyeaiyangzhan.com"/>
</bean>
<bean id="weiAroundMethod" class="com.shiyanlou.spring.aop.advice.WeiAroundMethod"/>
<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*URL.*</value>
</list>
</property>
<property name="advice" ref="weiAroundMethod"/>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean>
</beans>
以上配置中只要bean 的id符合*Service
就会自动创建Proxy,所以,可以使用一下代码获取Proxy:
context = new ClassPathXmlApplicationContext("SpringAopAdvice.xml");
CustomerService cust = (CustomerService) context.getBean("customerService");
运行结果如下:
*************************
Customer name: weihouye
*************************
Method Name: printURL
Method Arguments: []
WeiBeforeMethod: Before method hi wei!!!
Customer URL: www.weihouyeaiyangzhan.com
WeiAfterMethod: After Method hi wei!!
*************************
3、利用DefaultAdvisorAutoProxyCreator自动创建Proxy
利用DefaultAdvisorProxyCreator
自动创建代理,任何匹配Advisor的Bean,都会自动创建Proxy实现AOP,所以慎用。
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="customerService" class="com.shiyanlou.spring.aop.advice.CustomerService">
<property name="name" value="weihouye"/>
<property name="url" value="www.weihouyeaiyangzhan.com"/>
</bean>
<bean id="weiAroundMethod" class="com.shiyanlou.spring.aop.advice.WeiAroundMethod"/>
<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*URL.*</value>
</list>
</property>
<property name="advice" ref="weiAroundMethod"/>
</bean>
<!--利用DefaultAdvisorAutoProxyCreator自动创建代理-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
</beans>
以上配置,容器中的bean,有方法名和.*URL.*
匹配的方法,在使用时,都会自动创建Proxy,如下获取代理:
context = new ClassPathXmlApplicationContext("SpringAopAdvice.xml");
CustomerService cust = (CustomerService) context.getBean("customerService");
实验结果同上。
4、个人总结
从实验结果看来,Spring Aop编程有三个要点:
- 四种通知Advices 定义了,拦截器的代码在被拦截类的方法中执行的时机:
Before Advice
—— 被代理类方法执行前,执行拦截器代码;After Returning Advice
—— 被代理类方法执行后(且无异常);After Throwing Advice
—— 被代理类方法执行抛出异常后;Around advice
—— 环绕通知,结合了以上三种
- Pointcut 定义了要拦截被代理类的哪个方法,通过正则匹配或者方法名匹配。Advisor 将 Advice 和 Pointcut 结合组成独立的单元,可以传给
ProxyFactoryBean
- 第三点要定义容器中的哪些bean需要作为被代理类,为它创建相应的代理类;
- 手动创建代理 —— 创建
ProxyFactoryBean
,指定target
属性(被代理的bean),指定interceptorNames
(拦截器)- 自动创建代理 —— 利用
BeanNameAutoProxyCreator
、DefaultAdvisorAutoProxyCreator
自动创建Proxy