【引述】spring in action学习笔记 2 AOP的例子-DoubleEO

【引用】spring in action学习笔记 2 AOP的例子-DoubleEO

5.实现aop的例子 
1.首先先来点预备类,咱定义一个表演的接口,代码如下: 

Java代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. public interface Perform {  
  2. void perform();  
  3. }  

    就一个方法,表演节目,然后再定义2个实现类,ShowBoy和ShowGirl 
Java代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1.     public class ShowBoy implements Perform{  
  2.     public void perform() {  
  3.         System.out.println("表演街舞");  
  4.     }  
  5. }  
  6.     public class ShowGirl implements Perform{  
  7.     public void perform() {  
  8.         System.out.println("表演肚皮舞");  
  9.     }  
  10. }  

这些要bean要让spring来帮我们管理,所以要把他们写到spring的配置文件中。现在先不写,一会统一写。 
    现在该干正事了,首先就是定义通知,也就是说,想在表演节目的时候插入什么事情呢? 
我们定义一个观众类,让他们在表演的时候,做一些动作。 
Java代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. public class Audience {  
  2.     public Audience() {  
  3.     }  
  4.     public void takeSeat(){  
  5.         System.out.println("观众们找到自己的座位,都坐下来了");  
  6.     }  
  7.     public void turnOffMobilePhone(){  
  8.         System.out.println("请所有观众确定手机已经关闭");  
  9.     }  
  10.     public void appluad(){  
  11.         System.out.println("观众们大声鼓掌,啪啦啪啦啪啦");  
  12.     }  
  13.     public void demandRefund(){  
  14.         System.out.println("演的太差了,我们要退钱!");  
  15.     }  
  16. }  

从这个类定义的方法大概可以看出,找座位和关手机应该是表演前发生的,鼓掌应该是表演后发生的,而要求退钱应该是表演发生意外后发生的。 
    总结一下,Spring的aop通知有5种形式 
  Before:org.springframework.aop.MethodBeforeAdvice,这个接口代表方法之前。 
  After-returning: org.springframework.aop.AfterReturningAdvice,这个代表返回后 
  After-throwing:org.springframework.aop.ThrowsAdvice,代表抛出异常后。 
  Around:org.aopalliance.intercept.MethodInterceptor,代表一个方法的周围。 
  Introduction:org.springframework.aop.IntroductionInterceptor,代表引入 
    现在来定义真正的通知,通知不是包含应该干什么和何时干吗,那就写把。 
Java代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. public class AudienceAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{  
  2.     private Audience audience;  
  3.   
  4.     public void setAudience(Audience audience) {  
  5.         this.audience = audience;  
  6.     }  
  7.   
  8.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  9.         audience.takeSeat();  
  10.         audience.turnOffMobilePhone();  
  11.     }  
  12.   
  13.     public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {  
  14.         audience.appluad();  
  15.     }  
  16.     public void afterThrowing(Throwable throwable){  
  17.         audience.demandRefund();  
  18.     }  
  19. }  

其中该干什么在 Audience中定义的,而什么时候,就是这些接口所实现的方法,带有before,after等。都表明了什么时候。 

    有了通知,就该定义切点了把,切点直接在配置文件里定义,这时,也顺便把通知和目标类一起定义到xml文件中。切点是干嘛的,切点是定义应该在哪些方法用切面的,他有2种定义方式,一种是用正则表达式,来匹配想要的方法,另一种是用aspectJ切点表达式。 
<!--定义目标类,也就是想被织入通知的类--> 
   
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <bean id="showBoy" class="com.spring.springcase.ShowBoy"/>  
  2.     <bean id="showGirl" class="com.spring.springcase.ShowGirl"/>  
  3.     <!--定义了通知中的功能,此类做为通知的从属类-->  
  4.     <bean id="audience" class="com.spring.springcase.Audience"/>  
  5.     <!--定义通知-->  
  6.     <bean id="audienceAdvice" class="com.spring.springcase.AudienceAdvice">  
  7.         <property name="audience" ref="audience"/>  
  8.     </bean>  
  9.      <!--定义切点,声明想要的方法:spring提供的定义切点方式-->  
  10.     <bean id="springpointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">  
  11.         <property name="pattern" value=".*perform"/>  
  12.     </bean>  
  13.   
  14.     <!--定义切点,aspectJ定义的切点方式-->  
  15.     <bean id="asPectJpoincut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">  
  16.         <property name="expression" value="execution(* Performer+.perform(..))"/>  
  17.     </bean>  

    现在切点也有了,就要搞完整切面了,完整切面也叫通知者,直接在xml中配置. 
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <!--定义完整切面,把定义好的切点和通知放进来就行了 spring定义方式-->  
  2.   <bean id="audienceAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
  3.        <property name="pointcut" ref="springpointcut"/>  
  4.        <property name="advice" ref="audienceAdvice"/>  
  5.    </bean>  
  6.    <!--定义完整切面,aspectJ定义方式-->  
  7.   <bean id="audienceAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">  
  8.        <property name="expression" value="execution(* Performer+.perform(..))"/>  
  9.        <property name="advice" ref="audienceAdvice"/>  
  10.    </bean>  


切面有了,基本的事情都好了,现在可以想办法把切面用到目标类上了,aop的原理是通过代理实现的,所以我们要声明代理bean了。 

首先,之前的ShowBoy和ShowGirl要改在xml中声明的id,他们现在是目标类,所以XML要改为
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <bean id="showBoyTarget" class="com.spring.springcase.ShowBoy"/>  
  2.     <bean id="showGirlTarget" class="com.spring.springcase.ShowGirl"/>  

现在制作一个代理
   
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <!--定义代理类,第一个参数是要代理的对象,第2个是使用的切面,第3个是实现的接口-->  
  2.    <bean id="showBoy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  3.        <property name="target" ref="showBoyTarget"/>  
  4.        <property name="interceptorNames">  
  5.            <list>  
  6.                <value>audienceAdvice</value>  
  7.            </list>  
  8.        </property>  
  9.        <property name="proxyInterfaces">  
  10.            <list>  
  11.                <value>com.spring.springcase.Perform</value>  
  12.            </list>  
  13.        </property>  
  14.    </bean>  


现在就把代理类伪装成showBoy了,为什么要这样,上一篇已经说过了。 
    现在测试一下 
Java代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. public class performTest extends TestCase{  
  2.     ApplicationContext ctx;  
  3.     @Override  
  4.     protected void setUp() throws Exception {  
  5.         ctx = new ClassPathXmlApplicationContext("spring-springcase.xml");  
  6.     }  
  7.     public void testShowBoy(){  
  8.         Perform perform = (Perform)ctx.getBean("showBoy");  
  9.         perform.perform();  
  10.     }  
  11. }  
  12. 打印结果:  
  13. 观众们找到自己的座位,都坐下来了  
  14. 请所有观众确定手机已经关闭  
  15. 表演街舞  
  16. 观众们大声鼓掌,啪啦啪啦啪啦  

如果现在要showGirl也代理,那么就要写它的xml: 
 
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <bean id="showGril" class="org.springframework.aop.framework.ProxyFactoryBean">  
  2.         <property name="target" ref="showGirlTarget"/>  
  3.         <property name="interceptorNames">  
  4.             <list>  
  5.                 <value>audienceAdvice</value>  
  6.             </list>  
  7.         </property>  
  8.         <property name="proxyInterfaces">  
  9.             <list>  
  10.                 <value>com.spring.springcase.Perform</value>  
  11.             </list>  
  12.         </property>  
  13.     </bean>  

如果再有一个呢,那非给把你累死,发现定义代理,大部分代码是一样的,就是引用的目标类不同,这时就要把共有的代码抽出来,自己去写变换的那部分。 
定义一个代理的父类,实现了2个参数,其中的目标类由自己写。 
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <bean id="audienceProxyBase" class="org.springframework.aop.framework.ProxyFactoryBean" abstract="true">  
  2.        <property name="interceptorNames">  
  3.            <list>  
  4.                <value>audienceAdvice</value>  
  5.            </list>  
  6.        </property>  
  7.        <property name="proxyInterfaces">  
  8.            <list>  
  9.                <value>com.spring.springcase.Perform</value>  
  10.            </list>  
  11.        </property>  
  12.    </bean>  


重新声明伪装类就简单很多了 
Xml代码 【引述】spring in action学习笔记 2 AOP的例子-DoubleEO
  1. <bean id="showBoy" parent="audienceProxyBase">  
  2.     <property name="target" ref="showBoyTarget"/>  
  3. </bean>  
  4. <bean id="showGirl" parent="audienceProxyBase">  
  5.     <property name="target" ref="showGirlTarget"/>  
  6. </bean>  

但是,许多人还是觉得要定义这么一大堆东西,还是太麻烦,没错,我也觉得是麻烦,spring有更好的实现方式。明天再接着总结。 

引用

写的不错,不过应该吧xml文件的名称写清楚 

所有的xml都写在spring-springcase.xml这个配置文件中