静态代理&动态代理
原文地址:http://blog.csdn.net/partner4java/article/details/7048879
静态AOP和动态AOP。
静态代理:
代理对象与被代理对象必须实现同一个接口。
demo:
- package cn.partner4java.proxy.staticproxy;
- /**
- * 静态代理,统一接口
- * @author partner4java
- *
- */
- public interface IHello {
- /**
- * 可以带来的统一方法
- * @param name
- */
- public void hello(String name);
- }
- package cn.partner4java.proxy.staticproxy;
- /**
- * 被代理的对象,需要借助代理对象加入日志
- * @author partner4java
- *
- */
- public class HelloSpeaker implements IHello {
- public void hello(String name) {
- System.out.println("Hello " + name);
- }
- }
- package cn.partner4java.proxy.staticproxy;
- /**
- * 代理对象,给被代理对象添加日志
- */
- public class HelloProxy implements IHello {
- private IHello iHello;
- public HelloProxy(IHello iHello) {
- super();
- this.iHello = iHello;
- }
- public void hello(String name) {
- System.out.println("记录日志");
- iHello.hello(name);
- }
- }
- package cn.partner4java.proxy.staticproxy;
- /**
- * 调用
- * @author partner4java
- *
- */
- public class ProxyDemo {
- public static void main(String[] args) {
- IHello iHello = new HelloProxy(new HelloSpeaker());
- iHello.hello("long");
- }
- }
动态代理:
动态代理区别于静态带来实现的地方在于织入过程是在运行时动态进行的。自己实现一般实现java.lang.reflect.InvocationHandler接口。
例子:
- package cn.partner4java.proxy.dynamicproxy;
- public interface IHello {
- public void hello(String name);
- }
- package cn.partner4java.proxy.dynamicproxy;
- /**
- * 被代理的对象,需要借助代理对象加入日志
- * @author partner4java
- *
- */
- public class HelloSpeaker implements IHello {
- public void hello(String name) {
- System.out.println("Hello " + name);
- }
- }
- package cn.partner4java.proxy.dynamicproxy;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- /**
- * 动态代理对象
- * @author partner4java
- *
- */
- public class LogHandler implements InvocationHandler {
- private Object delegate;
- public Object bind(Object delegate){
- this.delegate = delegate;
- return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
- delegate.getClass().getInterfaces(), this);
- }
- /**
- * 代理对象,这里面还可以改变原有的方法
- */
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- Object result = null;
- try {
- System.out.println("添加日志");
- result = method.invoke(delegate, args);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- package cn.partner4java.proxy.dynamicproxy;
- /**
- * 测试
- * @author partner4java
- *
- */
- public class ProxyDemo {
- public static void main(String[] args) {
- LogHandler logHandler = new LogHandler();
- IHello iHello = (IHello) logHandler.bind(new HelloSpeaker());
- iHello.hello("long");
- }
- }
------------------------------------------------------------------
利用ProxyFactory连接CGLIB简单实现AOP:
加入包aopalliance.jar\cglib-nodep-2.1_3.jar
demo:
- package cn.partner4java.proxy.proxyfactory;
- /**
- * 被代理的对象
- * @author partner4java
- *
- */
- public class MessageWriter {
- public void writeMessage(){
- System.out.println("world!");
- }
- }
- package cn.partner4java.proxy.proxyfactory;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- /**
- * 装饰者<br/>
- * MethodInterceptor接口是对方法调用连接点实现包围通知的AOP联盟标准接口
- * @author partner4java
- *
- */
- public class MessageDecorator implements MethodInterceptor{
- public Object invoke(MethodInvocation invocation) throws Throwable {
- System.out.print("Hello ");
- Object retVal = invocation.proceed();
- return retVal;
- }
- }
- package cn.partner4java.proxy.proxyfactory;
- import org.springframework.aop.framework.ProxyFactory;
- /**
- * 调用组装
- * 这里最重要的部分是我们使用ProxyFactory来创建一个目标对象代理,同时织入通知
- * @author partner4java
- *
- */
- public class HelloWorldWeaver {
- public static void main(String[] args) {
- //目标
- MessageWriter target = new MessageWriter();
- //create the proxy
- ProxyFactory proxyFactory = new ProxyFactory();
- proxyFactory.addAdvice(new MessageDecorator());
- proxyFactory.setTarget(target);
- //获取返回被代理的目标
- MessageWriter proxy = (MessageWriter) proxyFactory.getProxy();
- target.writeMessage();
- System.out.println("---");
- proxy.writeMessage();
- // 后台打印:
- // world!
- // ---
- // World world!
- }
- }