Spring AOP Step-by-step(1)

阅读更多

: 概念

Aspect,常译为“方面”,是对施加于不同Class实例的相同问题域的抽象。

: 参与者

(1) Pointcut: ”方面”的切点--用来决定是否执行Advice的谓词

(2) Advice: 方面的处理

(3) Advisor: PointcutAdvice的粘合剂

三:Step-by-step

1 Bean: IShape, Rectange, Circle(代码忽略)

   IShape.java:IShape接口

  1. package com.rainsoft.exercise1.aop;   
  2.   
  3. /**  
  4.  * Interface for shapes like "circle", "rectange" etc.  
  5.  * @author RainS.Yang  
  6.  * @version 1.0  
  7.  */  
  8. public interface IShape {   
  9.     void draw();   
  10. }  
Rectange.java:IShape的一个实现
  1. package com.rainsoft.exercise1.aop;   
  2.   
  3. /**  
  4.  * @author RainS.Yang  
  5.  * @version 1.0  
  6.  */  
  7. public class Rectange implements IShape {   
  8.     public void draw() {   
  9.         System.out.println("Rectange is drawn.");   
  10.     }   
  11. }  

Note:

(1) SpringAOP Framework缺省通过Dynamic Proxy实现,所以要定义接口;而且接口编程被强烈建议

2 Advice:以MethodBeforeAdvice为例

    TimestampMethodBeforeAdvice.java

  1. package com.rainsoft.exercise1.aop;   
  2.   
  3. import java.lang.reflect.Method;   
  4. import java.text.SimpleDateFormat;   
  5. import java.util.Date;   
  6.   
  7. import org.springframework.aop.MethodBeforeAdvice;   
  8.   
  9. /**  
  10.  * Print timestamp to stdout when enter specified methods(pointcuts)  
  11.  *   
  12.  * @author RainS.Yang  
  13.  * @version 1.0  
  14.  */  
  15. public class TimestampMethodBeforeAdvice implements MethodBeforeAdvice {   
  16.     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");   
  17.   
  18.     public void before(Method method, Object[] args, Object target)   
  19.             throws Throwable {   
  20.         System.out.println(df.format(new Date()) + ":entering method - "    
  21.                 + method.getName());   
  22.     }   
  23. }   

Note:

(1) Spring AOP Frame内置5种Advice,所有Advice都从org.aopalliance.aop.Advice继承来

 

  Spring AOP Frmaework支持的Advice
Before advice

org.springframework.aop.BeforeAdvice;

org.springframework.aop.MethodBeforeAdvice

After returning advice org.springframework.aop.AfterReturningAdvice
After throwing advice org.springframework.aop.ThrowsAdvice
After (finally) advice ?
Aroud advice ?

3 Config:

spring-aop-demo.xml
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <beans>    
  4.       
  5.   <bean id="rect" class="org.springframework.aop.framework.ProxyFactoryBean">    
  6.     <property name="proxyInterfaces">    
  7.       <value>com.rainsoft.exercise1.aop.IShapevalue>    
  8.     property>    
  9.     <property name="target">    
  10.       <ref local="rectTarget"/>    
  11.     property>    
  12.     <property name="interceptorNames">    
  13.       <list>    
  14.         <value>timestampAdvisorvalue>    
  15.       list>    
  16.     property>    
  17.   bean>  
  18.      
  19.     
  20.   <bean id="rectTarget" class="com.rainsoft.exercise1.aop.Rectange">bean>  
  21.   
  22.       
  23.   <bean id="timestampAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    
  24.     <property name="advice">    
  25.       <ref local="timestampAdvice"/>    
  26.     property>    
  27.     <property name="pattern">    
  28.       <value>com\.rainsoft\.exercise1\.aop\.IShape\.drawvalue>    
  29.     property>    
  30.   bean>  
  31.      
  32.       
  33.   <bean id="timestampAdvice" class="com.rainsoft.exercise1.aop.TimestampMethodBeforeAdvice"/>      
  34. beans>  

4 主程序

Main.java
  1. package com.rainsoft.exercise1.aop;   
  2.   
  3. import org.springframework.context.ApplicationContext;    
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  5.   
  6. /**  
  7.  * Demo how to use Spring AOP  
  8.  * @author RainS.Yang  
  9.  * @version 1.0  
  10.  */  
  11. public class Main {   
  12.     public static void main(String[] args) {   
  13.         try {   
  14.             ApplicationContext ctx = new FileSystemXmlApplicationContext("spring-aop-demo.xml");   
  15.             IShape shape = (IShape)ctx.getBean("rect");   
  16.             shape.draw();   
  17.         }   
  18.         catch (Exception ex) {   
  19.             ex.printStackTrace();   
  20.         }   
  21.     }   
  22. }   

5 运行结果

  1. 2006-12-05 01/32/03:entering method - draw   
  2. Rectange is drawn.  

你可能感兴趣的:(AOP,Spring,Bean,编程,XML)