赞
踩
利用Java的反射技术(Java Reflection),在运行时创建一个实现某些给定接口的新类(也称“动态代理类”)及其实例(对象),代理的是接口(Interfaces),不是类(Class),也不是抽象类。在运行时才知道具体的实现,spring aop就是此原理。
首先:写一个接口
package com.openlab.mapper;
public interface usermapper {
public void show();
}
接着写一个类实现接口:
package com.openlab.mapper;
public class Usermapperlmp implements usermapper {
public void show() {
// TODO Auto-generated method stub
System.out.println("感谢你特别邀请,来见证你的爱情");
}
}
然后写一个类实现InvocationHandler接口:
package com.openlab.mapper; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class Myhandler implements InvocationHandler { private Object target;//定义一个对象 public Myhandler(Object target){ //给对象赋值 this.target=target; } //method方法arg2方法参数 public Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable { //实现接口方法 // TODO Auto-generated method stub System.out.println("想念");//调用方法前显示 Object res=method.invoke(target, arg2);//调用target类的method方法参数arg2 System.out.println("错过");//调用方法后显示 return res; } }
然后测试类:
package com.openlab.Proxy; import java.lang.reflect.Proxy; import org.junit.Test; import com.openlab.mapper.Myhandler; import com.openlab.mapper.Usermapperlmp; import com.openlab.mapper.usermapper; public class Proxytext { @Test public void test(){ Usermapperlmp u=new Usermapperlmp();//创建对象 Class<?>[] classes=u.getClass().getInterfaces();//获取对象的所有接口 usermapper user=(usermapper)Proxy.newProxyInstance (Proxytext.class.getClassLoader(),classes, new Myhandler(u)); //Proxytext.class.getClassLoader()获取当前类的加载器 user.show();//调用show()方法 } }
Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
第一个参数(ClassLoader loader 表示用哪个类的加载器
第二参数Class<?>[] interfaces 动态类需要实现的接口个数
第三参数InvocationHandler h 动态代理方法在执行时,会调用h里面的invoke方法去执行
返回类型是Object类型,需要强转为我们需要的接口类型;
上面代码中,代理u对象,调用show方法时,自动执行Myhandler中的invoke方法。
结果:
我们会发现打印结果和我们的代码位置是一样的;
连接点:类里面那些方法可以被增强,这些
方法称为连接点
切入点:实际被增强的方法
通知:实际增强的逻辑部分 类型:前置通知 后置通知 环绕通知 异常通知 最终通知
切面:动作,把通知应用到切入点过程
1.通过注解方式
Advice(通知、切面): 某个连接点所采用的处理逻辑,也就是向连接点注入的代码, AOP在特定的切入点上执行的增强处理。
@Before: 标识一个前置增强方法,相当于BeforeAdvice的功能.
@After: final增强,不管是抛出异常或者正常退出都会执行.
@AfterReturning: 后置增强,似于AfterReturningAdvice, 方法正常退出时执行.
@AfterThrowing: 异常抛出增强,相当于ThrowsAdvice.
@Around: 环绕增强,相当于MethodInterceptor.
表达式标签
execution():用于匹配方法执行的连接点
args(): 用于匹配当前执行的方法传入的参数为指定类型的执行方法
this(): 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;
target(): 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
within(): 用于匹配指定类型内的方法执行;
@args():于匹配当前执行的方法传入的参数持有指定注解的执行;
@target():用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;
@within():用于匹配所以持有指定注解类型内的方法;
@annotation:用于匹配当前执行方法持有指定注解的方法;
切面类:
package com.openlab.Proxy; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect//注解切面类 public class Userproxy { @Pointcut(value="execution(* com.openlab.*.*.*(..))")//Pointcut(切入点): JoinPoint的集合,是程序中需要注入Advice的位置的集合,指明Advice要在什么样的条件下才能被触发,在程序中主要体现为书写切入点表达式。 public void Poin(){ } @Before(value="Poin()")//前置通知 public void before(){ System.out.println("遇见你好幸运 方法调用之前"); } @After(value="execution(* com.openlab.*.*.*(..))")//后置通知 public void after(){ System.out.println("错过 方法调用之后"); } @AfterReturning(value="Poin()")//返回通知(最终通知) public void afterreturn(){ System.out.println("别了 方法返回"); } @AfterThrowing(value="Poin()")//异常通知 public void aferthrow(){ System.out.println(); } @Around(value="Poin()")//环绕通知 public void aroud(ProceedingJoinPoint joinpoint) throws Throwable{ System.out.println("想你 前环绕");
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。