javascript
Spring-AOP 引介切面
- 概述
- 引介切面類繼承關系
- IntroductionAdvisor接口的兩個實現類
- DefaultIntroductionAdvisor的構造函數
- 實例
概述
之前的博文介紹了 Spring-AOP 通過配置文件實現 引介增強 ,引介切面是引介增強的封裝器,通過引介切面可以很容易的為現有對象添加任何接口的實現。
引介切面類繼承關系
IntroductionAdvisor 和 PointcutAdvisor不同,IntroductionAdvisor 僅有一個類過濾器ClassFilter而沒有MethodMatcher,因為引介切面是類級別的,而Poincut的切點是方法級別的。
IntroductionAdvisor接口的兩個實現類
DefaultIntroductionAdvisor
引介切面最常用的實現類DeclareParentsAdvisor
用于實現使用AspectJ語言的DeclareParent注解表示的引介切面。
DefaultIntroductionAdvisor的構造函數
public DefaultIntroductionAdvisor(Advice advice)
通過一個增強創建的引介切面,引介切面將為目標對象增強對象中所有接口的實現
public DefaultIntroductionAdvisor(Advice advice, IntroductionInfo
introductionInfo)
通過一個增強和一個IntroductionInfo創建引介切面,目標對象小實現哪些接口由introduction對象的getInterfaces()方法標識public DefaultIntroductionAdvisor(DynamicIntroductionAdvice advice,
Class<?> intf)
通過一個IE增強和一個指定的接口類創建引介切面,僅為目標對象新增intf接口的實現
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
其余代碼同 Spring-AOP 通過配置文件實現 引介增強
我們通過DefaultIntroductionAdvisor配置引介切面,更加簡潔、清晰
<?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"><!-- 目標類 --><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配置方式配置引介切面。
總結
以上是生活随笔為你收集整理的Spring-AOP 引介切面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP 流程切面
- 下一篇: Spring-AOP 自动创建代理