AOP和动态代理

JDK动态代理

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.wadata.hdc.dxs.service.TransportService;

/**
* 动态代理处理器
* @author yong.zhang
*
*/
public class HessianTransportHandler implements InvocationHandler {

private Object target;

public HessianTransportHandler(Object target){
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("前=====");
Object result=method.invoke(target, args);
System.out.println("后=====");
return result;
}

//创建代理
public static Object createProxy(Object target){
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), 
new HessianTransportHandler(target));
}

public static void main(String[] args) {
TransportService createProxy = (TransportService) HessianTransportHandler.createProxy(new HessianTransportService());
createProxy.transport(null);
}

}

使用SPRING通知模块化横切点

http://lrf8933.blog.163.com/blog/static/68973963201102542010524/


你可能感兴趣的:(动态代理)