import org.junit.Test;
import com.matt.mattspring.aop.AdvisedSupport;
import com.matt.mattspring.aop.JdkDynamicAopProxy;
import com.matt.mattspring.aop.TargetSource;
import com.matt.mattspring.context.ApplicationContext;
import com.matt.mattspring.context.ClassPathXmlApplicationContext;
import com.matt.mattspring.test.HelloWorldService;
import com.matt.mattspring.test.HelloWorldServiceImpl;
public class JdkDynaicAopProxyTest {
@Test
public void testInterceptor() throws Exception
{
// -------- helloWorldService without AOP
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("resource.xml");
HelloWorldService helloWorldService = (HelloWorldServiceImpl) applicationContext.getBean("helloWorldService");
helloWorldService.helloWorld();
// -------- helloWorldService with AOP
// 1.设置被代理对象(Joinpoint)
AdvisedSupport advisedSupport = new AdvisedSupport();
TargetSource targetSource = new TargetSource(helloWorldService, HelloWorldService.class);
advisedSupport.setTargetSource(targetSource);
// 2.设置拦截器(Advice)
TimerInterceptor timerInterceptor = new TimerInterceptor();
advisedSupport.setMethodInterceptor(timerInterceptor);
// 3.创建代理(Proxy)
JdkDynamicAopProxy jdkDynamicAopProxy = new JdkDynamicAopProxy(advisedSupport);
HelloWorldService helloWorldServiceProxy = (HelloWorldService) jdkDynamicAopProxy.getProxy();
// 4.基于AOP的调用
helloWorldServiceProxy.helloWorld();
}
}
package com.matt.mattspring.aop;
import org.aopalliance.intercept.MethodInterceptor;
/**
* 代理相关的元数据
* @author Administrator
*
*/
public class AdvisedSupport {
private TargetSource targetSource;
private MethodInterceptor methodInterceptor;
public TargetSource getTargetSource() {
return targetSource;
}
public void setTargetSource(TargetSource targetSource) {
this.targetSource = targetSource;
}
public MethodInterceptor getMethodInterceptor() {
return methodInterceptor;
}
public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
this.methodInterceptor = methodInterceptor;
}
}
package com.matt.mattspring.aop;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
public class ReflectiveMethodInvocation implements MethodInvocation{
private Object target;
private Method method;
private Object[] args;
public ReflectiveMethodInvocation(Object target, Method method,
Object[] args) {
super();
this.target = target;
this.method = method;
this.args = args;
}
@Override
public AccessibleObject getStaticPart() {
return this.method;
}
@Override
public Object getThis() {
return this.target;
}
@Override
public Object proceed() throws Throwable {
return this.method.invoke(this.target, this.args);
}
@Override
public Object[] getArguments() {
return this.args;
}
@Override
public Method getMethod() {
return this.method;
}
}
package com.matt.mattspring.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.aopalliance.intercept.MethodInterceptor;
/**
* 基于jdk的动态代理
* @author Administrator
*
*/
public class JdkDynamicAopProxy implements AopProxy, InvocationHandler{
private AdvisedSupport advised;
public JdkDynamicAopProxy(AdvisedSupport advised) {
super();
this.advised = advised;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
MethodInterceptor methodInterceptor = this.advised.getMethodInterceptor();
return methodInterceptor.invoke(new ReflectiveMethodInvocation(this.advised.getTargetSource().getTarget(), method, args));
}
@Override
public Object getProxy() {
return Proxy.newProxyInstance(getClass().getClassLoader(),
new Class[]{this.advised.getTargetSource().getTargetClass()},
this);
}
}
package com.matt.mattspring.aop;
public class TargetSource {
private Class targetClass;
private Object target;
public TargetSource(Object target, Class targetClass) {
super();
this.targetClass = targetClass;
this.target = target;
}
public Class getTargetClass() {
return targetClass;
}
public Object getTarget() {
return target;
}
}
package com.matt.mattspring.test.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class TimerInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
long time = System.nanoTime();
System.out.println("Invocation of Method " + invocation.getMethod().getName() + " start!");
Object proceed = invocation.proceed();
System.out.println("Invocation of Method " + invocation.getMethod().getName() + " end! task " +
(System.nanoTime() - time) + " nanoseconds");
return proceed;
}
}