spring2.5学习笔记(六):AOP(1)用JDK中的Proxy实现与用CGlib实现的比较
这里我们就需要用到cglib-nodep-2.1_3.jar这个包
下面代码是演示的是一个用户权限判断,并赋予这个用户相应的操作。这里我做的是这样的用户权限设置:只要用户名不为空,那么就赋予它操作目标类中的所有方法的权利
代码一:
package cn.itcast.service;public interface IPersonService {public void save(String name);}
package cn.itcast.service.aop;import java.lang.reflect.Method;import cn.itcast.service.impl.personServiceBean;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;public class CGlibProxyFactory implements MethodInterceptor{private Object targetObject;public Object createProxyIntance(Object targetObject){this.targetObject = targetObject;Enhancer enhancer = new Enhancer();enhancer.setSuperclass(this.targetObject.getClass());//CGlib是集成目标类,然后覆盖目标类中的非final方法,这样达到代理的效果enhancer.setCallback(this);return enhancer.create();}public Object intercept(Object proxy, Method method, Object[] args,MethodProxy methodProxy) throws Throwable {personServiceBean bean =(personServiceBean) this.targetObject;Object result = null;if(bean.getUser()!=null){//advice -->AOP术语:前置通知 try {result = methodProxy.invoke(targetObject, args);//afteradvice-->AOP术语:后置通知} catch (Exception e) {//Exceptionadvice-->AOP术语:例外通知}finally{//finallyadvice-->AOP术语:最终通知}}return result;}}
package cn.itcast.service.aop;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import cn.itcast.service.impl.personServiceBean2;public class JDKProxyFactory implements InvocationHandler{private Object targetObject;public Object createProxyIntance(Object targetObject){this.targetObject =targetObject;return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), this.targetObject.getClass().getInterfaces(), this);}public Object invoke(Object arg0, Method arg1, Object[] arg2)throws Throwable {personServiceBean2 bean =(personServiceBean2) this.targetObject;Object result = null;if(bean.getUser()!=null){ result = arg1.invoke(targetObject, arg2);}return result;}}
package cn.itcast.service.impl;import cn.itcast.service.IPersonService;public class personServiceBean {private String user ;public String getUser() {return user;}public personServiceBean() {}public personServiceBean(String user) {this.user = user;}public String getPersonName(Integer personId) {return "postino";}public void save(String name) {System.out.println("这是save方法");}}
package cn.itcast.service.impl;import cn.itcast.service.IPersonService;public class personServiceBean2 implements IPersonService {private String user ;public String getUser() {return user;}public personServiceBean2() {}public personServiceBean2(String user) {this.user = user;}public String getPersonName(Integer personId) {return "postino";}public void save(String name) {System.out.println("这是save方法");}public void update(String name, Integer personId) {System.out.println("这是update方法");}}
package junit.test;import static org.junit.Assert.*;import org.junit.BeforeClass;import org.junit.Test;import cn.itcast.service.aop.CGlibProxyFactory;import cn.itcast.service.aop.JDKProxyFactory;import cn.itcast.service.impl.personServiceBean;import cn.itcast.service.impl.personServiceBean2;import cn.itcast.service.IPersonService;public class AOPTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void proxyTest(){JDKProxyFactory jdkpf = new JDKProxyFactory();IPersonService ipersonService = (IPersonService)jdkpf.createProxyIntance(new personServiceBean2(""));ipersonService.save("");}@Test public void proxyTest2(){CGlibProxyFactory jdkpf = new CGlibProxyFactory();personServiceBean service = (personServiceBean)jdkpf.createProxyIntance(new personServiceBean(""));service.save("");}}