今天学习了关于CGLIB实现动态代理的实现demo,结合网上资料写了个demo供以后自己作参考
1,数据bean
/**
* Person.java
* Created at 2014年8月25日
* Created by mengfanyuan
* Copyright (C) 2014 SHANGHAI KALVENMENG, All rights reserved.
*/
package com.mfy.test.cglib.bean;
/**
* <p>
* ClassName: Person
*</p>
* <p>
* Description: TODO
* </p>
* <p>
* Author: mengfanyuan
* Email:kalvenmeng@163.com
* </p>
* <p>
* Date: 2014年8月25日
* </p>
*/
public class Person {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2,服务service
/**
* PersonServiceBean.java
* Created at 2014年8月25日
* Created by mengfanyuan
* Copyright (C) 2014 SHANGHAI KALVENMENG, All rights reserved.
*/
package com.mfy.test.cglib.service.imp;
import com.mfy.test.cglib.bean.Person;
/**
* <p>
* ClassName: PersonServiceBean
*</p>
* <p>
* Description: TODO
* </p>
* <p>
* Author: mengfanyuan
* Email:kalvenmeng@163.com
* </p>
* <p>
* Date: 2014年8月25日
* </p>
*/
public class PersonServiceBean {
private String user;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public PersonServiceBean() {
super();
// TODO Auto-generated constructor stub
}
public PersonServiceBean(String user) {
super();
this.user = user;
}
public void save(Person person) {
System.out.println("执行PerServiceBean的save方法");
}
}
3,代理实现
/**
* CGLIBFactory.java
* Created at 2014年8月25日
* Created by mengfanyuan
* Copyright (C) 2014 SHANGHAI KALVENMENG, All rights reserved.
*/
package com.mfy.test.cglib.aop;
import java.lang.reflect.Method;
import com.mfy.test.cglib.service.imp.PersonServiceBean;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* <p>
* ClassName: CGLIBFactory
*</p>
* <p>
* Description: TODO
* </p>
* <p>
* Author: mengfanyuan
* Email:kalvenmeng@163.com
* </p>
* <p>
* Date: 2014年8月25日
* </p>
*/
public class CglibProxy implements MethodInterceptor {
private Object targetObject;
public CglibProxy(Object targetObject) {
this.targetObject = targetObject;
}
public static <T>T proxyTarget(T t) {
Enhancer en = new Enhancer();
en.setSuperclass(t.getClass());
en.setCallback(new CglibProxy(t));
T tt = (T)en.create();
return tt;
}
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("拦截前。。。");
Object o = arg1.invoke(targetObject, arg2);
System.out.println("拦截后。。。");
return o;
}
}
4,测试类
/**
* TestMain.java
* Created at 2014年8月25日
* Created by mengfanyuan
* Copyright (C) 2014 SHANGHAI KALVENMENG, All rights reserved.
*/
package com.mfy.test.cglib;
import com.mfy.test.cglib.aop.CglibProxy;
import com.mfy.test.cglib.bean.Person;
import com.mfy.test.cglib.service.imp.PersonServiceBean;
/**
* <p>
* ClassName: TestMain
*</p>
* <p>
* Description: TODO
* </p>
* <p>
* Author: mengfanyuan
* Email:kalvenmeng@163.com
* </p>
* <p>
* Date: 2014年8月25日
* </p>
*/
public class TestMain {
public static void main(String[] args) {
PersonServiceBean p = CglibProxy.proxyTarget(new PersonServiceBean("user"));
p.save(new Person());
}
}
5,输出:
拦截前。。。
执行PerServiceBean的save方法
拦截后。。。
这样就实现对 PersonServiceBean 中方法的动态代理
参考:用CGLIB动态代理