AOP 深入了解
具体深入详解:
http://www.iteye.com/topic/480860
AOP
代理实现方式:java.lang.reflect.Proxy
实现步骤:
1、继承import java.lang.reflect.InvocationHandler
具体代码:
package com.royzhou.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy implements InvocationHandler {
private Object targetObject;
public Object createProxyObject(Object targetObject) {
this.targetObject = targetObject;
//生成代理对象
return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),this.targetObject.getClass().getInterfaces(),this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
UserBean userBean = (UserBean) targetObject;
String userName = userBean.getUserName();
Object result = null;
//权限判断
if(userName!=null && !"".equals(userName)) {
//调用目标对象的方法
result = method.invoke(targetObject, args);
}
return result;
}
}
2、调用
package com.royzhou.aop;
public class TestProxy {
public static void main(String[] args) {
JDKProxy jProxy = new JDKProxy();
IUserBean userBean = (IUserBean) jProxy.createProxyObject(new UserBean("royzhou"));
userBean.addUser();
}
}