Spring AOP 拦截指定方法

在Spring AOP中,有3个常用的概念,Advices、Pointcut、Advisor,解释如下,

Advices:表示一个method执行前或执行后的动作。

Pointcut:表示根据method的名字或者正则表达式去拦截一个method。

Advisor:Advice和Pointcut组成的独立的单元,并且能够传给proxy factory 对象。

package testaop;

public class CustomerService {
	 private String name;  
	    private String url;  
	      
	    public void printName(){  
	        System.out.println("Customer  name "+ name);  
	    }  
	      
	    public void printURL() {  
	        System.out.println("Customer website : " + this.url);  
	    }  
	  
	    public void printThrowException() {  
	        throw new IllegalArgumentException();  
	    }  
	  
	      
	    public String getName() {  
	        return name;  
	    }  
	    public void setName(String name) {  
	        this.name = name;  
	    }  
	    public String getUrl() {  
	        return url;  
	    }  
	    public void setUrl(String url) {  
	        this.url = url;  
	    }  
}
接着在创建一个实现了MethodInterceptor的类,必须调用“methodInvocation.proceed();” 继续在原来的方法执行,否则原来的方法将不会执行。
package testaop;

import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class CustomerInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		 System.out.println("Method name : "  
	                + methodInvocation.getMethod().getName());  
	          
	        System.out.println("Method arguments : "  
	                + Arrays.toString(methodInvocation.getArguments()));  
	  
	        // same with MethodBeforeAdvice    相当于  MethodBeforeAdvice
	        System.out.println("CustomerService: Before method CustomerService!");  
	  
	        try {  
	            // proceed to original method call  调用原方法,即调用CustomerService中的方法
	            Object result = methodInvocation.proceed();  
	  
	            // same with AfterReturningAdvice  相当于 AfterReturningAdvice
	            System.out.println("CustomerService : Before after CustomerService!");  
	  
	            return result;  
	  
	        } catch (IllegalArgumentException e) {  
	            // same with ThrowsAdvice  相当于 ThrowsAdvice
	            System.out.println("CustomerService : Throw exception CustomerService!");  
	            throw e;  
	        }  
	}

}
配置spring-aop.xml
  
  
     
     
     
          
          
      
      
    
      
      
      
          
          
             

                customerAdvisor 
              
          
     
    
    
      
          
      
  
      
          
          
      
    
  
  
最后测试测试程序如下
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import testaop.CustomerService;

/**
 * 测试类
 * @author Administrator
 *
 */
public class Test {

	public static void main(String[] args) {
		
		 // TODO Auto-generated method stub  
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");  
        CustomerService obj = (CustomerService) context.getBean("customerInterceptorProxy");  
         
        System.out.println("****************");  
        obj.printName();  
        System.out.println("****************");  
        obj.printURL();  
        System.out.println("****************");  
		
		
	}
}
输出结果:
****************
Method name : printName
Method arguments : []
CustomerService: Before method CustomerService!
Customer  name my name is youshuai
CustomerService : Before after CustomerService!
****************
Customer website : http://www.remote3c.com
****************





你可能感兴趣的:(Spring)