aop 笔记 引入

引入和其它类型的spring通知有所不同.其它类型的通知是在方法调用的周围织入到不同的连接点.而引入则是影响整个类.
引入让你能动态地建立复合对象.
spring 通过一个特殊的方法拦截接口IntroductionInterceptor
                     
java 代码
 
  1. boolean implementsInterface(Class intf);  
  2. 这个方法判断是否是引入的接口  
要引入的接口
java 代码
 
  1. package aop.introduction;  
  2.   
  3. public interface IChange {  
  4.         public boolean ichange();  
  5. }  
现在我们实现一个introductionInterceptor
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.aopalliance.intercept.MethodInvocation;  
  4. import org.springframework.aop.IntroductionInterceptor;  
  5.   
  6.   
  7. public class IChangeAdvisora implements IntroductionInterceptor,IChange{  
  8.   
  9.     public Object invoke(MethodInvocation m) throws Throwable {  
  10.         if(implementsInterface(m.getMethod().getDeclaringClass())){  
  11.             return m.getMethod().invoke(this, m.getArguments());
  12.             //通过MethodInvocation 和invoke代理要引入的方法
  13.         }else{  
  14.             return m.proceed();  //委托其它方法,不是引入,就像那个环绕通知
  15.         }  
  16.     }  
  17.   
  18.     public boolean implementsInterface(Class intf) {  
  19.         return intf.isAssignableFrom(IChange.class);  
  20.     }  
  21.   
  22.     public boolean ichange() {  
  23.         System.out.println("intrduction");  
  24.         return false;  
  25.     }  
  26.   
  27. }  
spring 为我们提供一个方便类的处理大多数的应用.DelegatingIntroductInterceptor
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.springframework.aop.support.DelegatingIntroductionInterceptor;  
  4.   
  5. public class IChangeAdvisor extends DelegatingIntroductionInterceptor   
  6.                    implements IChange  {  
  7.   
  8.     public boolean ichange() {  
  9.         // TODO 自动生成方法存根  
  10.         System.out.println("ichage");  
  11.       
  12.         return false;  
  13.     }     
  14. }  //比起上面那个类算得比较简洁,DelegatingIntroductionInterceptor 替我们实现invoke()
如果你想实现方法的拦截调用invoke方法
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.aopalliance.intercept.MethodInvocation;  
  4. import org.springframework.aop.support.DelegatingIntroductionInterceptor;  
  5.   
  6. public class IChangeAdvisorb extends DelegatingIntroductionInterceptor implements IChange {  
  7.   
  8.     public boolean ichange() {  
  9.         // TODO 自动生成方法存根  
  10.         return false;  
  11.     }  
  12.     public Object invoke(MethodInvocation e) throws Throwable{  
  13.         String name = e.getMethod().getName();  
  14.         if(name.indexOf("set") == 0){   //这里拦截set方法  
  15.             throw new IllegalMonitorStateException();  
  16.         }  
  17.         return super.invoke(e);  
  18.     }  
  19.   
  20. }  
下面是配制
java 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="targetbean" class="aop.introduction.TagetBean" />  
  5.   
  6.     <bean id="iChangeAdvisor" class="aop.introduction.IChangeAdvisorb" />  
  7.  //这里配制引入advisor,看你用得是那个  
  8.     <bean id="advisor"  
  9.         class="org.springframework.aop.support.DefaultIntroductionAdvisor">  
  10.         <constructor-arg>  
  11.             <ref bean="iChangeAdvisor" />  
  12.         </constructor-arg>  
  13.     </bean>  
  14.     <bean id="bean"  
  15.         class="org.springframework.aop.framework.ProxyFactoryBean">  
  16.         <property name="target">  
  17.             <ref bean="targetbean" />  
  18.         </property>  
  19.         <property name="interceptorNames">  
  20.             <list>  
  21.                 <value>advisor</value>  
  22.             </list>  
  23.         </property>  
  24.         <property name="proxyInterfaces">  
  25.             <value>aop.introduction.IChange</value>  
  26.         </property>  
  27.         <property name="proxyTargetClass">  
  28.             <value>true</value>  
  29.         </property>  
  30.     </bean>  
  31. </beans>  
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  5.   
  6. public class IntroductionTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ctx = new FileSystemXmlApplicationContext(  
  10.                 "./src/applicationContext.xml");  
  11.   
  12.         TagetBean bean = (TagetBean) ctx.getBean("bean");  
  13. //                 ((IChange) ctx.getBean("bean")).ichange();  
  14.        IChange mod = (IChange) bean;  
  15.   
  16.         // test interfaces  
  17.         System.out.println("Is TargetBean?: " + (bean instanceof TagetBean));  
  18.         System.out.println("Is IsModified?: " + (bean instanceof IChange));  
  19.   
  20.         // test is modified implementation  
  21. //        mod.ichange();  
  22.        // bean.setName("ddd");  
  23.         System.out.println("Has been modified?: " + mod.ichange());  
  24.     }  
  25. }             
  26.          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值