在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<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customerService" class="testaop.CustomerService">
<property name="name" value="my name is youshuai" />
<property name="url" value="http://www.remote3c.com" />
</bean>
<bean id="customerInterceptor" class="testaop.CustomerInterceptor"/>
<bean id="customerInterceptorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<!-- <value>customerInterceptor</value>拦截该类中所有方法 -->
<value>customerAdvisor</value> <!-- 拦截该类中指定方法 -->
</list>
</property>
</bean>
<bean id="customerYiibaicut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedName" value="printName" />
</bean>
<bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="customerYiibaicut" />
<property name="advice" ref="customerInterceptor" />
</bean>
</beans>
最后测试测试程序如下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
****************