首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring2.5学习札记(六):AOP(1)用JDK中的Proxy实现与用CGlib实现的比较

2012-10-07 
spring2.5学习笔记(六):AOP(1)用JDK中的Proxy实现与用CGlib实现的比较这里我们就需要用到cglib-nodep-2.1_

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);}


代码二 CGlibProxyFactory.java和JDKProxyFactory.java
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;}}


代码三:personServiceBean.java和personServiceBean2.java
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方法");}}



测试类:AOPTest.java
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("");}}


学习总结:
1:JDKProxy拦截是,通过新建一个Proxy对象来实现的,里面传入三个参数,一个目标类的类加载器,一个目标类实现了的接口,一个目标类对象Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), this.targetObject.getClass().getInterfaces(), this);

2:CGlib是通过定义Enhancer类的对象,然后覆盖目标类的非final方法实现的集成,在通过回调

3:如果是用JDKProxy实现拦截,那么目标类必须是面向接口编程。而CGlib则不需要



热点排行