`
freewxy
  • 浏览: 337064 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用CGLIB实现AOP功能与AOP概念解释 .

阅读更多

 第三方创建代理对象的框架cglib,目标类可以不实现接口

cglib(Code Generation Library)是一个强大的,高性能,高质量的Code生成类库。它可以在运行期扩展Java类与实现Java接口。
cglib封装了asm,可以在运行期动态生成新的class。
cglib用于AOP,jdk中的proxy必须基于接口,cglib却没有这个限制

可以生成目标类的子类,并重写父类非final修饰符的方法

CGLib:http://www.blogjava.net/stone2083/archive/2008/03/16/186615.html

1、  创建被拦截对象,该对象没有实现接口:

package com.wxy.service.impl;

public class CopyOfPeopleServiceBean {

    private String user = null;

    public CopyOfPeopleServiceBean() {
    }

    public CopyOfPeopleServiceBean(String user) {
        this.setUser(user);
    }

    public String getPeopleName(Integer peopleId) {
        System.out.println("我是getPeopleName()方法");
        return "wxy";
    }

    public void save(String name) {
        System.out.println("我是save()方法,save:" + name);
    }

    public void update(String name, Integer peopleId) {
        System.out.println("我是update()方法");
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

}

 

2、  编写cglib代理工厂,实现代理功能:

package com.wxy.aop;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import com.wxy.service.impl.CopyOfPeopleServiceBean;

/**
*   创建代理对象
*    
*   @create-time     2011-8-19   上午09:07:53   
*   @revision          $Id
*/
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());//将目标类设置为代理对象的父类,可以覆盖所有非final方法进行覆盖
        enhancer.setCallback(this);//设置回调用对象为本身

        return enhancer.create();
    }

    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
                                                                                                throws Throwable {

        CopyOfPeopleServiceBean bean = (CopyOfPeopleServiceBean) this.targetObject;
        Object result = null;//环绕通知
        if (bean.getUser() != null) {
            //.....advice()-->前置通知
            try {
                result = methodProxy.invoke(targetObject, args);
                //.....afteradvice()-->后置通知
            } catch (RuntimeException e) {
                //.....exceptionadvice()-->例外通知
            } finally {
                //.....finallyadvice()-->最终通知
            }
        }

        return result;
    }

}

 

3、  测试:

public class AOPTest {
    public static void main(String[] args) {
        /*   JDKProxyFactory factory = new JDKProxyFactory();
           PeopleService service = (PeopleService) factory.createProxyInstance(new PeopleServiceBean(
               "wxy"));

           service.save("wxy");*/

        CGlibProxyFactory factory = new CGlibProxyFactory();
        CopyOfPeopleServiceBean service = (CopyOfPeopleServiceBean) factory
            .createProxyIntance(new CopyOfPeopleServiceBean("wxy"));
        service.save("cglib-wxy");
        CopyOfPeopleServiceBean service1 = (CopyOfPeopleServiceBean) factory
            .createProxyIntance(new CopyOfPeopleServiceBean());//没有权限
        service1.save("cglib-wxy");
    }

}

 

4、  结果:

public class AOPTest {
    public static void main(String[] args) {
        /*   JDKProxyFactory factory = new JDKProxyFactory();
           PeopleService service = (PeopleService) factory.createProxyInstance(new PeopleServiceBean(
               "wxy"));

           service.save("wxy");*/

        CGlibProxyFactory factory = new CGlibProxyFactory();
        CopyOfPeopleServiceBean service = (CopyOfPeopleServiceBean) factory
            .createProxyIntance(new CopyOfPeopleServiceBean("wxy"));
        service.save("cglib-wxy");
        CopyOfPeopleServiceBean service1 = (CopyOfPeopleServiceBean) factory
            .createProxyIntance(new CopyOfPeopleServiceBean());//没有权限
        service1.save("cglib-wxy");
    }

}

 

 

一些AOP概念:

Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象。

joinpoint(连接点):指那些被拦截到得点,在spring中,这些点是指方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类构造器。

Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义。

Advice(通知):所谓通知是指拦截到joinpoint之后要做的事情就是通知:异常通知、前置通知、后置通知、环绕通知、最终通知。

Target(目标):代理的目标对象

Weave(织入):把将aspect应用到target对象并导致proxy对象创建的过程称为织入。

Introduction(引入):在不修改类代码的前提下,Introduction可以运行为类动态的添加一些方法或者Field

 

http://blog.csdn.net/iamtheevil/article/details/6700495

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics