Spring-AOP 自动创建代理之DefaultAdvisorAutoProxyCreator
导读
Spring-AOP 自动创建代理之BeanNameAutoProxyCreator
Spring-AOP 自动创建代理之DefaultAdvisorAutoProxyCreator
Spring-AOP 自动创建代理之AnnotationAwareAspectJAutoProxyCreator
概述
我们知道切面Advisor是切点和增强的复合体,Advisor本身已经包含了足够的信息,如横切逻辑及连接点。
DefaultAdvisorAutoProxyCreator能够扫描Advisor,并将Advisor自动织入匹配的目标Bean中,即为匹配的目标Bean自动创建代理。
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
在 Spring-AOP 静态正则表达式方法匹配切面通过ProxyFactoryBean为waiter和seller配置了代理,如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 配置切面:静态正则表达式方法匹配切面 -->
<!-- Waiter目标类 -->
<bean id="waiterTarget" class="com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor.Waiter"/>
<!-- Seller目标类 -->
<bean id="sellerTarget" class="com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor.Seller"/>
<!-- 前置增强 -->
<bean id="greetBeforeAdvice" class="com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor.GreetBeforeAdivce"/>
<!-- 切面 -->
<bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
p:advice-ref="greetBeforeAdvice"
p:pattern=".*greet.*" > <!-- 向切面注入一个前置增强 , 用正则表达式定义目标类全限定方法名的匹配模式串 。-->
</bean>
<!-- 通过父bean,配置公共的信息 -->
<bean id="parent" abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="regexpAdvisor"
p:proxyTargetClass="true"/>
<!-- waiter代理 -->
<bean id="waiter" parent="parent" p:target-ref="waiterTarget"/>
<!-- seller代理 -->
<bean id="seller" parent="parent" p:target-ref="sellerTarget"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
在这里引入DefaultAdvisorAutoProxyCreator为容器中所有带 “greet”方法名的目标Bean自动创建代理,配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!--通过Advisor自动创建代理-->
<!-- 目标Bean -->
<bean id="waiter" class="com.xgj.aop.spring.advisor.autoCreateProxy.DefaultAdvisorProxyCreator.Waiter"/>
<bean id="seller" class="com.xgj.aop.spring.advisor.autoCreateProxy.DefaultAdvisorProxyCreator.Seller"/>
<!-- 增强 -->
<bean id="greetingBeforeAdvice" class="com.xgj.aop.spring.advisor.autoCreateProxy.DefaultAdvisorProxyCreator.GreetingBeforeAdvice"/>
<!-- 切面 -->
<bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
p:patterns=".*greet.*"
p:advice-ref="greetingBeforeAdvice"/>
<!-- 代理 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
最后一行,用DefaultAdvisorAutoProxyCreator定义一个Bean,它负责将容器中的Advisor织入匹配的目标Bean中。
测试类:
package com.xgj.aop.spring.advisor.autoCreateProxy.DefaultAdvisorProxyCreator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DefaultAdvisorProxyCreatorTest {
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/aop/spring/advisor/autoCreateProxy/DefaultAdvisorProxyCreator/conf-defaultAdvisorProxyCreator.xml");
Waiter waiter = ctx.getBean("waiter", Waiter.class);
waiter.greetTo("XiaoGongJiang");
waiter.serverTo("XiaoGongJiang");
System.out.println("\n");
Seller seller = ctx.getBean("seller", Seller.class);
seller.greetTo("XiaoGongJiang");
seller.serverTo("XiaoGongJiang");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
运行结果
2017-08-21 16:28:37,239 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb6dcf9: startup date [Mon Aug 21 16:28:37 BOT 2017]; root of context hierarchy
2017-08-21 16:28:37,388 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/autoCreateProxy/DefaultAdvisorProxyCreator/conf-defaultAdvisorProxyCreator.xml]
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.DefaultAdvisorProxyCreator.Waiter.greetTo
How are you XiaoGongJiang ?
Waiter Greet To XiaoGongJiang
Waiter Server To XiaoGongJiang
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.DefaultAdvisorProxyCreator.Seller.greetTo
How are you XiaoGongJiang ?
Seller Greet To XiaoGongJiang
Seller Server To XiaoGongJiang
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
从运行结果中,只有greetTo方法被织入了增强,由此可见,增强被正确的织入匹配的连接点中。
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/77466528
- 点赞
- 收藏
- 关注作者
评论(0)