本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~
浅谈spring——切面(七)这一节提到平日要借助ProxyFactoryBean建创织入切面的代理子类,虽然对标目类进行了强增,但是增加了很多额定的配置。
spring供给主动代理机制,可以帮我们从琐烦的工作中解救出来。其实现机制借助于BeanPostProcessor的主动代理建创器的实现类。
主动代理建创器的承继关系:
代理器有三类:
1. 基于Bean的名字的主动代理建创器,例如BeanNameAutoProxyCreator.java
2. 基于Advisor(切面)匹配机制的主动代理建创器。对spring容器中的全部的Advisor扫描,并将其应用到匹配的Bean中。例如DefaultAdvisorAutoProxyCreator.java
3. 基于Bean中的AspjectJ解注签标的主动代理建创器,例如AnnotationAwareAspectJAutoProxyCreator.java
全部的主动代理建创器,都是实现了BeanPostProcessor。spring容器在实例化Bean时,BeanPostProcessor会对其加工,对满意匹配则规的Bean主动建创代理对象。
BeanNameAutoProxyCreator代码示例:
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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 通普方法名匹配切面 --> <bean id="waiter" class="tome.sample.Advisor.Waiter" /> <bean id="seller" class="tome.sample.Advisor.Seller" /> <bean id="greetingAdvice" class="tome.sample.Advisor.GreetingBeforeAdvice" /> <!-- 通过Bean称名主动建创代理 --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" p:interceptorNames="greetingAdvice" p:optimize="true"> <property name="beanNames" value="*er" /> </bean> </beans>
注意:
beanNames可以应用 * 作通配符,也可以应用体具的bean名字,如
<property name="beanNames" value="waiter,seller" />
p:interceptorNames指定一个或多个强增Bean的称名,可是以强增bean,也可是以切面bean
p:optimize 制强应用CGLib动态代理
DefaultAdvisorAutoProxyCreator代码实例:
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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 通普方法名匹配切面 --> <bean id="waiter" class="tome.sample.Advisor.Waiter" /> <bean id="seller" class="tome.sample.Advisor.Seller" /> <bean id="greetingAdvice" class="tome.sample.Advisor.GreetingBeforeAdvice" /> <bean id="greetingAdvisor" class="tome.sample.Advisor.GreetingAdvisor"> <property name="advice" ref="greetingAdvice" /> </bean> <!--通过Advisor主动建创代理--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> </beans> </beans>
测试类:
public static void main(String[] args) { String configPath = "tomge/sample/Advisor/beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter waiter = (Waiter) ctx.getBean("waiter"); Seller seller = (Seller) ctx.getBean("seller"); waiter.greetTo("Tom1"); waiter.serveTo("Tom2"); seller.greetTo("Tom3"); }
结果:
-----------begin---------------
GreetingBeforeAdvice is successfully called! tome.sample.Advisor.Waiter.greetTo
How are you!Tom1!
-----------end---------------
waiter greet to Tom1!
waiter serving Tom2!
seller greet to Tom3!
文章结束给大家分享下程序员的一些笑话语录:
雅虎最擅长的不是开通新业务,是关闭旧业务。
来源:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/29/3050612.html