一,Cglib动态代理
给一个普通的类进行动态代理,以一个普通的bean为例,给其动态新增属性:
使用cglib-nodep的包
/**
* CGlib动态代理,给普通对象进行代理
* @author hwt
*
*/
public class DynamicBean {
//实体对象
private Object instance;
//属性
private BeanMap beanMap;
/**
* propertyMap 属性集合
* @param propertyMap
*/
public DynamicBean(Map<String, Class> propertyMap){
this.instance = genarateBean(propertyMap);
this.beanMap = beanMap.create(this.instance);
}
/**
* 给bean的属性设置值
* @param name 属性名
* @param value 属性值
*/
public void setValue(String name,Object value){
beanMap.put(name, value);
}
/**
* 取出bean的属性值
* @param name 属性名
* @return 属性值
*/
public Object getValue(String name){
return beanMap.get(name);
}
/**
* 得到bean对象
* @return
*/
public Object getBean(){
return this.instance;
}
/**
* 将属性加入到bean中
* @param propMap
* @return
*/
private Object genarateBean(Map<String, Class> propMap){
BeanGenerator generator = new BeanGenerator();
if (propMap != null) {
Set<String> keySet = propMap.keySet();
for (Iterator<String> it = keySet.iterator();it.hasNext();) {
String key = it.next();
generator.addProperty(key, propMap.get(key));
}
}
return generator.create();
}
}
测试类:
public static void main(String[] args) {
//bean的属性集合
Map<String, Class> propertyMap = new HashMap<String, Class>();
propertyMap.put("age", Integer.class);
propertyMap.put("name", String.class);
//设置属性值
DynamicBean proxy = new DynamicBean(propertyMap);
proxy.setValue("age", 23);
proxy.setValue("name", "黄文韬");
System.out.println(proxy.getValue("name"));
System.out.println(proxy.getValue("age"));
Object object = proxy.getBean();
Class clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
System.out.println(field.getName());
}
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
}
}
如果bean有继承关系,也可以使用cglib来代理,只需要在DynamicBean的generateBean中的方法中新增一条语句:
private Object genarateBean(Map<String, Class> propMap){
BeanGenerator generator = new BeanGenerator();
//设置父类
generator.setSuperclass(Person.class);
if (propMap != null) {
Set<String> keySet = propMap.keySet();
for (Iterator<String> it = keySet.iterator();it.hasNext();) {
String key = it.next();
generator.addProperty(key, propMap.get(key));
}
}
return generator.create();
}
父类:
public class Person {
private String userName;
private String password;
private boolean enable;
//setter getter方法....
}
测试:
public static void main(String[] args) {
//bean的属性集合
Map<String, Class> propertyMap = new HashMap<String, Class>();
propertyMap.put("age", Integer.class);
propertyMap.put("name", String.class);
//设置属性值
DynamicBean proxy = new DynamicBean(propertyMap);
proxy.setValue("age", 23);
proxy.setValue("name", "黄文韬");
//父类的属性
proxy.setValue("userName", "hwt_211");
proxy.setValue("password", "123456");
proxy.setValue("enable", true);
System.out.println(proxy.getValue("name"));
System.out.println(proxy.getValue("age"));
//父类的属性
System.out.println(proxy.getValue("userName"));
System.out.println(proxy.getValue("password"));
System.out.println(proxy.getValue("enable"));
Object object = proxy.getBean();
Class clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
System.out.println(field.getName());
}
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
}
}
二,JDK的动态代理
给实现接口的类进行jdk的动态代理,以简单的AOP实现为例:
1,接口类:
public interface UserService {
public void add();
}
2.实现类:
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("add method");
}
}
3.方法调用处理类
public class MyInvocationHandler implements InvocationHandler {
//目标对象
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("before add method.....");
Object object = method.invoke(target, args);
System.out.println("after add method.....");
return object;
}
}
4.代理
public static void main(String[] args) {
UserService userServiceImpl = new UserServiceImpl();
//需要代理的接口
Class[] interfaces = {UserService.class};
//类加载器
ClassLoader classLoader = TestProxy.class.getClassLoader();
//方法调用处理器
InvocationHandler h = new MyInvocationHandler(userServiceImpl);
//为userServiceImpl加上代理
userServiceImpl = (UserService) Proxy.newProxyInstance(classLoader, interfaces, h);
userServiceImpl.add();
}