Java中的动态代理
@SuppressWarnings("rawtypes")
private static Object getProxy(final Object target)
{
Object collProxy = (Object) Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(), new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
long beginTime = System.currentTimeMillis(); ------------可以提取到一个接口中,这样就可更面向方面编程了。这样直接传一个对象参数
Object retValue = method.invoke(target, args);
long endTime = System.currentTimeMillis();--------------------------可以提取到一个接口中,这样就可更面向方面编程了。这样直接传一个对象参数
System.out.println("运行时间:" + (endTime - beginTime)); ------------可以提取到一个接口中,这样就可更面向方面编程了。这样直接传一个对象参数
return retValue;
}
});
return collProxy;
}
// 别一种方式写动态代理类
System.out.println("--------------------------------------------------------------");
final ArrayList target = new ArrayList();
Collection collProxy = (Collection)getProxy(target);
collProxy.add("aaa");
collProxy.add("bbb");
collProxy.add("ccc");
System.out.println(collProxy.size());
AOP
安全 事务 日志
StudentService ------|----------|------------|-------------
CourseService ------|----------|------------|-------------
MiscService ------|----------|------------|-------------
method1 method2 method3
{ { {
------------------------------------------------------切面
.... .... ......
------------------------------------------------------切面
} } }
------------------------------------------------------切面
func1 func2 func3
{ { {
.... .... ......
} } }
------------------------------------------------------切面
代理内部原理

posted on 2013-05-06 22:05 peter.peng 阅读(157) 评论(0) 收藏 举报