浅谈java代理
java代理,见名思意,就是代理某个java类,并执行这个java类中的方法,在Java中的主要作用是AOP编程(面向方面编程)
代理作用:
就是在某个类的方法执行之前或者之后插入某些动作,但又不想改变代码的原始内容。
应用java代理:
要想用java代理首先熟知一个类,那就是代理类,Proxy
这个类中的方法都是静态的所以直接用这个Proxy来调用就行了,下面是两个重要的方法
getProxyClass(ClassLoader loader, Class<?>... interfaces) :
这个方法需要得到这个这个InvaocationHandle,并将这个对象作为参数来执行Proxy的newInstance来获取这个代理的类
如下所示
InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance()); //得到执行句柄,这个类需要覆写invoke方法
Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});//得到带了类字节码,其中声明用那个累加器加载的这个类,一及接口
Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);//得到代理类对象
InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);
newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h):
这个方法不过是把上面的两步变成一步操作:
InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());//得到执行句柄,这个类需要覆写invoke方法
Advice animate = (Advice) Proxy.newProxyInstance(Advice.class.getClassLoader(),new Class[]{Advice.class}, handle);//得到代理类对象
最后再用得到的代理对象就可以执行了
完整的代码如下:
生成代理的对象
public class ProxyFactoryBean {
private Advice advice;
private Object target;
public Advice getAdvice() {
return advice;
}
public void setAdvice(Advice advice) {
this.advice = advice;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Object getProxy() {
// TODO Auto-generated method stub
Object proxy = Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advice.doBefore();
Object retVal = method.invoke(target, args);
advice.doAfter();
return retVal;
}
}
);
return proxy;
}
}
获取bean
public class BeanFactory {
Properties props = new Properties();
public BeanFactory(InputStream ips){
try {
props.load(ips);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name){
String className = props.getProperty(name);
Object bean = null;
try {
bean = Class.forName(className).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bean instanceof ProxyFactoryBean){
Object proxy = null;
ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
try {
Advice advice = (MyAdvice) Class.forName(props.getProperty(name+".advice")).newInstance();
Object target = Class.forName(props.getProperty(name+".target")).newInstance();
proxyFactoryBean.setAdvice(advice);
proxyFactoryBean.setTarget(target);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
proxy = proxyFactoryBean.getProxy();
return proxy;
}
return bean;
}
}
测试:
public class MyProxyTest {
public static void main(String[] args) throws Throwable {
// TODO Auto-generated method stub
InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);
//Advice animate = (Advice) Proxy.newProxyInstance(Advice.class.getClassLoader(),new Class[]{Advice.class}, handle);
animate.doBefore();
animate.doAfter();
}
}