码迷,mamicode.com
首页 > 其他好文 > 详细

实现aop: 动态代理和cglib

时间:2014-11-26 20:47:21      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   java   on   div   

*****

1,动态代理

public interface UserDao {
    void save();
}

public class UserDaoImpl implements UserDao {

    private String name;
    
    public void save() {
        System.out.println("save() is called, name: "+name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    
}

代理工厂

ProxyFactory.java

package com.maple.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.maple.dao.UserDao;
import com.maple.dao.impl.UserDaoImpl;

public class ProxyFactory implements InvocationHandler {

    private Object target;
    
    public Object createUserDao(Object target){
        this.target=target;
        return Proxy.newProxyInstance(this.target.getClass().getClassLoader(),
                        this.target.getClass().getInterfaces(), this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println(proxy.getClass().getName());
        Object result=null;
        UserDaoImpl userDao=(UserDaoImpl)this.target;
        if(userDao.getName()!=null){
            result=method.invoke(userDao, args);
        }else{
            System.out.println("the name is null");
        }
        return result;
    }

}

测试

@Test
    public void test() {
        ProxyFactory pf=new ProxyFactory();
        UserDaoImpl u=new UserDaoImpl();
        u.setName("maple");
        UserDao userDao=(UserDao) pf.createUserDao(u);
        userDao.save();
    }

输出

$Proxy5
save() is called, name: maple

2,cglib

UserDaoImpl2.java不实现接口

package com.maple.dao.impl;


public class UserDaoImpl2{

    private String name;
    
    public void save() throws InterruptedException {
        Thread.sleep(3000);
        System.out.println("save() is called, name: "+name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void raiseException(){
        throw new RuntimeException("this is test");
    }
    
}

CglibFactory.java

package com.maple.util;

import java.lang.reflect.Method;

import com.maple.dao.impl.UserDaoImpl2;

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

public class CglibFactory implements MethodInterceptor {

    private Object target;
    public Object createUserDao(Object target){
        this.target=target;
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
    
    @Override
    public Object intercept(Object proxy, Method method, Object[] args,
            MethodProxy methodProxy) throws Throwable {
        UserDaoImpl2 userDao=(UserDaoImpl2)target;
        System.out.println("begin");
        if(userDao.getName()!=null){
            method.invoke(target, args);
        }else{
            System.out.println("the name is null");
        }
        System.out.println("end");
        return null;
    }

}

测试代码:

@Test
    public void test2() throws InterruptedException{
        CglibFactory cf=new CglibFactory();
        UserDaoImpl2 temp=new UserDaoImpl2();
        UserDaoImpl2 userDao=(UserDaoImpl2)cf.createUserDao(temp);
        userDao.save();
        temp.setName("aa");
        userDao=(UserDaoImpl2)cf.createUserDao(temp);
        userDao.save();
    }

结果:

begin
the name is null
end
begin
save() is called, name: aa
end

*****

实现aop: 动态代理和cglib

标签:style   blog   io   ar   color   sp   java   on   div   

原文地址:http://www.cnblogs.com/qingmaple/p/4124784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!