之前的博文介绍了 Spring-AOP 通过配置文件实现 引介增强 ,引介切面是引介增强的封装器,通过引介切面可以很容易的为现有对象添加任何接口的实现。
IntroductionAdvisor 和 PointcutAdvisor不同,IntroductionAdvisor 仅有一个类过滤器ClassFilter而没有MethodMatcher,因为引介切面是类级别的,而Poincut的切点是方法级别的。
Class intf
)
通过一个IE增强和一个指定的接口类创建引介切面,仅为目标对象新增intf接口的实现代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
其余代码同 Spring-AOP 通过配置文件实现 引介增强
我们通过DefaultIntroductionAdvisor配置引介切面,更加简洁、清晰
<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">
<bean id="forumServiceTarget" class="com.xgj.aop.spring.advisor.introductionAdvisor.ForumService" />
<bean id="introductionAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg>
<bean class="com.xgj.aop.spring.advisor.introductionAdvisor.ControllablePerformaceMonitor"/>
constructor-arg>
bean>
<bean id="forumService" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="introductionAdvisor"
p:target-ref="forumServiceTarget"
p:proxyTargetClass="true" />
beans>
运行结果:
2017-08-20 19:02:30,492 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5f0101fb: startup date [Sun Aug 20 19:02:30 BOT 2017]; root of context hierarchy
2017-08-20 19:02:30,598 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/introductionAdvisor/conf-introductionAdvisor.xml]
模拟删除Forum记录:10
模拟删除Topic记录:1022
begin monitor...
模拟删除Forum记录:10
end monitor...
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.removeForum花费7421毫秒。
begin monitor...
模拟删除Topic记录:1022
end monitor...
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.removeTopic花费12468毫秒。
虽然引介切面和其他切面的配置有很大的不同,但却可以采用相似的Spring配置方式配置引介切面。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有