引入 cglib-nodep-2.1_3.ja包
cglib产生的代理类是目标类的子类
定义接口,让切面都继承它,方便加入到动态代理方法 的那个类中使用
在SalaryInterceptor类中使用 private List<Interceptor> interceptors;
1 package cn.itcast.cglibproxy.salary; 2 3 public interface Interceptor { 4 public void interceptor(); 5 }
Logger .java
1 package cn.itcast.cglibproxy.salary; 2 3 public class Logger implements Interceptor{ 4 5 @Override 6 public void interceptor() { 7 // TODO Auto-generated method stub 8 System.out.println("logging"); 9 } 10 11 }
其他类都继承 Interceptor接口
Privilege.java
1 package cn.itcast.cglibproxy.salary; 2 3 public class Privilege implements Interceptor{ 4 5 @Override 6 public void interceptor() { 7 // TODO Auto-generated method stub 8 System.out.println("privilege"); 9 } 10 11 }
Security.java
1 package cn.itcast.cglibproxy.salary; 2 3 public class Security implements Interceptor{ 4 5 @Override 6 public void interceptor() { 7 // TODO Auto-generated method stub 8 System.out.println("security"); 9 } 10 11 }
等等
目标对象
1 package cn.itcast.cglibproxy.salary; 2 3 public class SalaryManagerImpl { 4 public void showSalary() { 5 System.out.println("查看工资"); 6 } 7 }
核心
1 package cn.itcast.cglibproxy.salary; 2 3 import java.lang.reflect.Method; 4 import java.util.List; 5 6 import net.sf.cglib.proxy.Enhancer; 7 import net.sf.cglib.proxy.MethodInterceptor; 8 import net.sf.cglib.proxy.MethodProxy; 9 10 public class SalaryInterceptor implements MethodInterceptor{ 11 12 13 private Object target; 14 15 private List<Interceptor> interceptors; 16 17 public SalaryInterceptor(Object target,List<Interceptor> interceptors){ 18 this.target = target; 19 this.interceptors = interceptors; 20 } 21 //返回动态代理对象 22 public Object createProxy(){ //增强对象 23 Enhancer enhancer = new Enhancer(); 24 enhancer.setCallback(this); 25 enhancer.setSuperclass(this.target.getClass()); 26 return enhancer.create(); 27 } 28 //酷似jdk中的 invoke方法 29 //arg3中存放方法的代理相关参数 30 @Override 31 public Object intercept(Object arg0, Method arg1, Object[] arg2, 32 MethodProxy arg3) throws Throwable { 33 // TODO Auto-generated method stub 34 for(Interceptor interceptor:interceptors){ 35 interceptor.interceptor(); 36 } 37 arg1.invoke(this.target, arg2); 38 return null; 39 } 40 41 42 }
测试
1 package cn.itcast.cglibproxy.salary; 2 3 import java.lang.reflect.Proxy; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.junit.Test; 8 9 public class SalaryTest { 10 @Test 11 public void test(){ 12 Object target = new SalaryManagerImpl(); 13 Logger logger = new Logger(); 14 Security security = new Security(); 15 Privilege privilege = new Privilege(); 16 List<Interceptor> interceptors = new ArrayList<Interceptor>(); 17 interceptors.add(logger); 18 interceptors.add(security); 19 interceptors.add(privilege); 20 //返回的是目标类型的子类 21 SalaryInterceptor interceptor = new SalaryInterceptor(target, interceptors); 22 SalaryManagerImpl proxy = (SalaryManagerImpl)interceptor.createProxy(); 23 proxy.showSalary(); 24 } 25 }
aop:
1、切面
事务、日志、安全性框架、权限等都是切面 相当于例子中的 Logger Privilege
2、通知
切面中的方法就是通知 上面类的方法
3、目标类 SalaryManagerImpl类
4、切入点 在方法SalaryInterceptor 中的intercept方法中判断条件 ,什么条件调用目标方法
只有符合切入点,才能让通知和目标方法结合在一起
5、织入:
形成代理对象的方法的过程
好处:
事务、日志、安全性框架、权限、目标方法之间完全是松耦合的