AOP是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。使用反射生成JDK动态代理,可以实现面向切面的功能,即AOP,下面通过案例来展示反射在AOP中使用的作用:
[java]view plaincopyprint?
1. package com.xiaomo.reflex;
2.
3. import java.lang.reflect.InvocationHandler;
4. import java.lang.reflect.Method;
5. import java.lang.reflect.Proxy;
6.
7. public class AOPTest {
8.
9. interface Dog {
10.void info();
11.void run();
12.}
13.
14.static class GunDog implements Dog {
15.
16.@Override
17.public void info() {
18.System.out.println("I am a gundog!");
19.}
20.
21.@Override
22.public void run() {
23.System.out.println("I run fast!");
24.}
25.
26.}
27.
28.static class Authority {
29.//第一个拦截方法
30.public void first() {
31.System.out.println("------第一个切面方法------");
32.}
33.//第二个拦截方法
34.public void last() {
35.System.out.println("------最后一个切面方法------");
36.}
37.}
38.
39.static class MyInvocationHandler implements InvocationHandler {
40.//需要被代理的对象
41.private Object target;
42.
43.public void setTarget(Object target) {
44.this.target = target;
45.}
46.//执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法
47.public Object invoke(Object proxy, Method method, Object[] args)
48.throws Exception {
49.Authority authority = new Authority();
50.//执行权限对象中的in方法
51.authority.first();
52.System.out.println("正在执行的方法:" + method);
53.//if (args != null) {
54.//System.out.print("执行该方法时传入的实参:");
55.//for (Object val : args) {
56.//System.out.println(val);
57.//}
58.//} else {
59.//System.out.println("调用该方法没有实参!");
60.//}
61.System.out.print("执行结果:");
62.Object result = method.invoke(target, args);
63.//执行权限对象中的out方法
64.authority.last();
65.return result;
66.}
67.}
68.
69.static class MyProxyFactory {
70.//为制定的target生成动态代理对象
71.public static Object getProxy(Object target) throws Exception {
72.//创建一个MyInvocationHandler对象
73.MyInvocationHandler handler = new MyInvocationHandler();
74.//为MyInvocationHandler设置target对象
75.handler.setTarget(target);
76.//创建并返回一个动态代理
77.return Proxy.newProxyInstance(target.getClass().getClassLoader(),
78.target.getClass().getInterfaces(), handler);
79.}
80.}
81.
82.public static void main(String[] args) throws Exception {
83.//创建一个原始的GunDog对象,作为target
84.Dog target = new GunDog();
85.//以制定的target来创建动态代理对象
86.Dog dog = (Dog) MyProxyFactory.getProxy(target);
87.dog.info();
88.dog.run();
89.}
90.}
package com.xiaomo.reflex;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class AOPTest {
interface Dog {
void info();
void run();
}
static class GunDog implements Dog {
@Override
public void info() {
System.out.println("I am a gundog!");
}
@Override
public void run() {
System.out.println("I run fast!");
}
}
static class Authority {
//第一个拦截方法
public void first() {
System.out.println("------第一个切面方法------");
}
//第二个拦截方法
public void last() {
System.out.println("------最后一个切面方法------");
}
}
static class MyInvocationHandler implements InvocationHandler {
//需要被代理的对象
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Exception {
Authority authority = new Authority();
//执行权限对象中的in方法
authority.first();
System.out.println("正在执行的方法:" + method);
//if (args != null) {
//System.out.print("执行该方法时传入的实参:");
//for (Object val : args) {
//System.out.println(val);
//}
//} else {
//System.out.println("调用该方法没有实参!");
//}
System.out.print("执行结果:");
Object result = method.invoke(target, args);
//执行权限对象中的out方法
authority.last();
return result;
}
}
static class MyProxyFactory {
//为制定的target生成动态代理对象
public static Object getProxy(Object target) throws Exception {
//创建一个MyInvocationHandler对象
MyInvocationHandler handler = new MyInvocationHandler();
//为MyInvocationHandler设置target对象
handler.setTarget(target);
//创建并返回一个动态代理
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(), handler);
}
}
public static void main(String[] args) throws Exception {
//创建一个原始的GunDog对象,作为target
Dog target = new GunDog();
//以制定的target来创建动态代理对象
Dog dog = (Dog) MyProxyFactory.getProxy(target);
dog.info();
dog.run();
}
}
运行结果如下:
------第一个切面方法------
正在执行的方法:public abstract void com.xiaomo.reflex.AOPTest$Dog.info()
执行结果:I am a gundog!
------最后一个切面方法------
------第一个切面方法------
正在执行的方法:public abstract void com.xiaomo.reflex.AOPTest$Dog.run()
执行结果:I run fast!
------最后一个切面方法------