用纯用纯Java的方式实现Spring的切面技术
/*AOP中的一个重要等式:
* 切面=切点+通知
* advisor=cutpoint+advice
* 切面: 定义的一个拦截事件(动作)
* 切点: 要拦截哪些(个)类的哪些(个)方法
* 通知: 定义在方法的前面、后面、环绕、出异常 还是 正常返回的时候拦
*/
代码实现思路:
1.被代理对象
2.切点
技术入口:
JdkRegexpMethodPointcut cut2 = new JdkRegexpMethodPointcut();
cut2.setPattern(".*abc.*");
3.通知
4.切面 = 切点 + 通知
5.bean的代理工厂----生产出代理后的bean
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setTarget(p); //设置被代理对象
factory.addAdvisors(advisor,advisor2);
6.从代理工厂中取出代理后的bean
Person p2 = (Person) factory.getObject();
package cn.hncu.aop.v1;
import java.lang.reflect.Method;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import cn.hncu.aop.Person;
/*AOP中的一个重要等式:
* 切面=切点+通知
* advisor=cutpoint+advice
* 切面: 定义的一个拦截事件(动作)
* 切点: 要拦截哪些(个)类的哪些(个)方法
* 通知: 定义在方法的前面、后面、环绕、出异常 还是 正常返回的时候拦
*/
//用纯Java的方式实现Spring的切面技术
public class Demo1 {
@Test
public void demo(){
//1 被代理对象
Person p = new Person();
//2切点
JdkRegexpMethodPointcut cut = new JdkRegexpMethodPointcut();
cut.setPattern(".*run.*");
//3通知
Advice advice = new MethodBeforeAdvice() {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("在前面拦拦......");
}
};
Advice advice2 = new AfterReturningAdvice() {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("后面拦拦,你的返回值是:"+returnValue);
}
};
JdkRegexpMethodPointcut cut2 = new JdkRegexpMethodPointcut();
cut2.setPattern(".*abc.*");
cut2.setPattern(".*abc.*");
//4 切面 = 切点 + 通知
Advisor advisor = new DefaultPointcutAdvisor(cut, advice);
Advisor advisor2 = new DefaultPointcutAdvisor(cut2, advice2);
//5 bean的代理工厂----生产出代理后的bean
//ProxyFactory factory = new ProxyFactory();//低版本Spring用的
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setTarget(p); //设置被代理对象
factory.addAdvisors(advisor,advisor2);
//6 从代理工厂中取出代理后的bean
Person p2 = (Person) factory.getObject();
p2.run();
p2.abc();
}
@Test
public void demo2(){
//1 被代理对象
Person p = new Person();
//2切点
JdkRegexpMethodPointcut cut = new JdkRegexpMethodPointcut();
cut.setPattern(".*run.*");
//3通知
Advice advice = new AroundAdviceImpl();
//4 切面 = 切点 + 通知
Advisor advisor = new DefaultPointcutAdvisor(cut, advice);
//5 bean的代理工厂----生产出代理后的bean
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setTarget(p); //设置被代理对象
factory.addAdvisors(advisor);
//6 从代理工厂中取出代理后的bean
Person p2 = (Person) factory.getObject();
p2.run();
//p2.abc();
}
}
通知的实现类 实现MethodInterceptor接口
public class AroundAdviceImpl implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("前面拦截,做动作...");
Object returnValue = invocation.proceed();//放行
System.out.println("后面拦截,做动作...");
return returnValue;
}
}