`
chenqian
  • 浏览: 77822 次
  • 性别: Icon_minigender_1
  • 来自: 湘潭
社区版块
存档分类
最新评论

Spring AOP

    博客分类:
  • ssh
阅读更多
动态代理
public class MyHandler implements InvocationHandler {

    private Object target;

    public MyHandler(Object t) {
        target= t;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("hello!!");
        return method.invoke(target,args);
    }
}


public class ProxyTest {
    public static void main(String args[]) {
        Object target = ...;
        InvocationHandler handler = new MyHandler(target);
        //Class[] interfaces = target.getClass().getInterfaces();
        Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
        proxy...
    }
}


   public     class   BusinessProxy   implements   IBusiness      {   
     private  BusinessImple bi = null ;   
     public  BusinessProxy(BusinessImple bi) {   
         this .bi = bi;   
    }    
     public   void  doBusiness() {   
        System.out.println( " 事务、日志、权限等操作 " );   
        bi.doBusiness();   
        System.out.println( " 事务、日志、权限等操作 " );   
    }    
}

应用
  public     class   AOPAppDemo      {   
     public   static   void  main(String[] args) {   
        BusinessImpl bi = new  BusinessImple();   
        BusinessProxy proxy = new  BusinessProxy(bi);   
        proxy.doBusiness();   
    }    
} 
  
AOP三个关键点
advice   通知,是你想向别的程序内部不同的地方注入的代码。
pointcut 切入点,定义了需要注入advice的位置,通常是某个特定的类的一个 public方法。
advisor  是pointcut和advice的装配器,是将advice注入主程序中预定义位置的代码。

五种advice
调用前、调用后、前后环绕、抛出异常后、return后

三种切入点
动态、静态、自定义


实现一个before advice,这意味着advice的代码在被调用的public方法开始前被执行
package    com.company.springaop.test; 

import   java.lang.reflect.Method; 
import   org.springframework.aop.MethodBeforeAdvice; 

public     class   TestBeforeAdvice   implements   MethodBeforeAdvice  { 

    public     void   before(Method m, Object[] args, Object target) 
    throws   Throwable  { 
    System.out.println(  "  Hello world! (by   "   
          +     this  .getClass().getName() 
          +     "  )  "  ); 
  }  
} 


spring context配置
<? xml version = " 1.0 "  encoding = " UTF-8 " ?> 
<! DOCTYPE beans PUBLIC   " -//SPRING//DTD BEAN//EN "   " http://www.springframework.org/dtd/spring-beans.dtd " > 

< beans > 
   <!-- CONFIG --> 
   < bean id = " bean "   class = " org.springframework.aop.framework.ProxyFactoryBean " > 
     < property name = " proxyInterfaces " > 
       < value > com.company.springaop.test.Bean </ value > 
     </ property > 
     < property name = " target " > 
       < ref local = " beanTarget " /> 
     </ property > 
     < property name = " interceptorNames " > 
       < list > 
         < value > theAdvisor </ value > 
       </ list > 
     </ property > 
   </ bean > 

   <!-- CLASS --> 
   < bean id = " beanTarget "   class = " com.company.springaop.test.BeanImpl " /> 

   <!-- ADVISOR --> 
   <!-- Note: An advisor assembles pointcut and advice --> 
   < bean id = " theAdvisor "   class = " org.springframework.aop.support.RegexpMethodPointcutAdvisor " > 
     < property name = " advice " > 
       < ref local = " theBeforeAdvice " /> 
     </ property > 
     < property name = " pattern " > 
       < value > com\.company\.springaop\.test\.Bean\.theMethod </ value > 
     </ property > 
   </ bean > 

   <!-- ADVICE --> 
   < bean id = " theBeforeAdvice "   class = " com.company.springaop.test.TestBeforeAdvice " /> 
</ beans >   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics