Skip to content

Commit 5fef1c5

Browse files
committed
method interceptor by jdk dynamic proxy
1 parent 1737798 commit 5fef1c5

10 files changed

+212
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
package us.codecraft.tinyioc.aop;
22

3+
import org.aopalliance.intercept.MethodInterceptor;
4+
35
/**
46
57
*/
68
public class AdvisedSupport {
9+
10+
private TargetSource targetSource;
11+
12+
private MethodInterceptor methodInterceptor;
13+
14+
public TargetSource getTargetSource() {
15+
return targetSource;
16+
}
17+
18+
public void setTargetSource(TargetSource targetSource) {
19+
this.targetSource = targetSource;
20+
}
21+
22+
public MethodInterceptor getMethodInterceptor() {
23+
return methodInterceptor;
24+
}
25+
26+
public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
27+
this.methodInterceptor = methodInterceptor;
28+
}
729
}

src/main/java/us/codecraft/tinyioc/aop/AopProxyFactory.java

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import org.aopalliance.intercept.MethodInterceptor;
4+
5+
import java.lang.reflect.InvocationHandler;
6+
import java.lang.reflect.Method;
7+
import java.lang.reflect.Proxy;
8+
9+
/**
10+
11+
*/
12+
public class JdkDynamicAopProxy implements AopProxy, InvocationHandler {
13+
14+
private AdvisedSupport advised;
15+
16+
public JdkDynamicAopProxy(AdvisedSupport advised) {
17+
this.advised = advised;
18+
}
19+
20+
@Override
21+
public Object getProxy() {
22+
return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { advised.getTargetSource()
23+
.getTargetClass() }, this);
24+
}
25+
26+
@Override
27+
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
28+
MethodInterceptor methodInterceptor = advised.getMethodInterceptor();
29+
return methodInterceptor.invoke(new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method, args));
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import org.aopalliance.intercept.MethodInvocation;
4+
5+
import java.lang.reflect.AccessibleObject;
6+
import java.lang.reflect.Method;
7+
8+
/**
9+
10+
*/
11+
public class ReflectiveMethodInvocation implements MethodInvocation {
12+
13+
private Object target;
14+
15+
private Method method;
16+
17+
private Object[] args;
18+
19+
public ReflectiveMethodInvocation(Object target, Method method, Object[] args) {
20+
this.target = target;
21+
this.method = method;
22+
this.args = args;
23+
}
24+
25+
@Override
26+
public Method getMethod() {
27+
return method;
28+
}
29+
30+
@Override
31+
public Object[] getArguments() {
32+
return args;
33+
}
34+
35+
@Override
36+
public Object proceed() throws Throwable {
37+
return method.invoke(target, args);
38+
}
39+
40+
@Override
41+
public Object getThis() {
42+
return target;
43+
}
44+
45+
@Override
46+
public AccessibleObject getStaticPart() {
47+
return method;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
/**
4+
5+
*/
6+
public class TargetSource {
7+
8+
private Class targetClass;
9+
10+
private Object target;
11+
12+
public TargetSource(Object target, Class<?> targetClass) {
13+
this.target = target;
14+
this.targetClass = targetClass;
15+
}
16+
17+
public Class getTargetClass() {
18+
return targetClass;
19+
}
20+
21+
public Object getTarget() {
22+
return target;
23+
}
24+
}

src/test/java/us/codecraft/tinyioc/HelloWorldService.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,7 @@
33
/**
44
55
*/
6-
public class HelloWorldService {
6+
public interface HelloWorldService {
77

8-
private String text;
9-
10-
private OutputService outputService;
11-
12-
public void helloWorld(){
13-
outputService.output(text);
14-
}
15-
16-
public void setText(String text) {
17-
this.text = text;
18-
}
19-
20-
public void setOutputService(OutputService outputService) {
21-
this.outputService = outputService;
22-
}
8+
void helloWorld();
239
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package us.codecraft.tinyioc;
2+
3+
/**
4+
5+
*/
6+
public class HelloWorldServiceImpl implements HelloWorldService {
7+
8+
private String text;
9+
10+
private OutputService outputService;
11+
12+
@Override
13+
public void helloWorld(){
14+
outputService.output(text);
15+
}
16+
17+
public void setText(String text) {
18+
this.text = text;
19+
}
20+
21+
public void setOutputService(OutputService outputService) {
22+
this.outputService = outputService;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import org.junit.Test;
4+
import us.codecraft.tinyioc.HelloWorldService;
5+
import us.codecraft.tinyioc.context.ApplicationContext;
6+
import us.codecraft.tinyioc.context.ClassPathXmlApplicationContext;
7+
8+
/**
9+
10+
*/
11+
public class JdkDynamicAopProxyTest {
12+
13+
@Test
14+
public void testInterceptor() throws Exception {
15+
// --------- helloWorldService without AOP
16+
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("tinyioc.xml");
17+
HelloWorldService helloWorldService = (HelloWorldService) applicationContext.getBean("helloWorldService");
18+
helloWorldService.helloWorld();
19+
20+
// --------- helloWorldService with AOP
21+
// 1. 设置被代理对象(Joinpoint)
22+
AdvisedSupport advisedSupport = new AdvisedSupport();
23+
TargetSource targetSource = new TargetSource(helloWorldService, HelloWorldService.class);
24+
advisedSupport.setTargetSource(targetSource);
25+
26+
// 2. 设置拦截器(Advice)
27+
TimerInterceptor timerInterceptor = new TimerInterceptor();
28+
advisedSupport.setMethodInterceptor(timerInterceptor);
29+
30+
// 3. 创建代理(Proxy)
31+
JdkDynamicAopProxy jdkDynamicAopProxy = new JdkDynamicAopProxy(advisedSupport);
32+
HelloWorldService helloWorldServiceProxy = (HelloWorldService) jdkDynamicAopProxy.getProxy();
33+
34+
// 4. 基于AOP的调用
35+
helloWorldServiceProxy.helloWorld();
36+
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import org.aopalliance.intercept.MethodInterceptor;
4+
import org.aopalliance.intercept.MethodInvocation;
5+
6+
/**
7+
8+
*/
9+
public class TimerInterceptor implements MethodInterceptor {
10+
11+
@Override
12+
public Object invoke(MethodInvocation invocation) throws Throwable {
13+
long time = System.nanoTime();
14+
System.out.println("Invocation of Method " + invocation.getMethod().getName() + " start!");
15+
Object proceed = invocation.proceed();
16+
System.out.println("Invocation of Method " + invocation.getMethod().getName() + " end! takes " + (System.nanoTime() - time)
17+
+ " nanoseconds.");
18+
return proceed;
19+
}
20+
}

src/test/resources/tinyioc.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<property name="helloWorldService" ref="helloWorldService"></property>
1313
</bean>
1414

15-
<bean name="helloWorldService" class="us.codecraft.tinyioc.HelloWorldService">
15+
<bean name="helloWorldService" class="us.codecraft.tinyioc.HelloWorldServiceImpl">
1616
<property name="text" value="Hello World!"></property>
1717
<property name="outputService" ref="outputService"></property>
1818
</bean>

0 commit comments

Comments
 (0)