首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

反照实现 AOP 动态代理模式

2012-11-06 
反射实现 AOP 动态代理模式?这是我在网上看到的非常不错的反射实现AOP动态代理的技术文章。以下为原句:好长

反射实现 AOP 动态代理模式

?

这是我在网上看到的非常不错的反射实现AOP动态代理的技术文章。

以下为原句:

好长时间没有用过Spring了. 突然拿起书.我都发现自己对AOP都不熟悉了.
其实AOP的意思就是面向切面编程.
OO注重的是我们解决问题的方法(封装成Method),而AOP注重的是许多解决解决问题的方法中的共同点,是对OO思想的一种补充!
还是拿人家经常举的一个例子讲解一下吧:
比如说,我们现在要开发的一个应用里面有很多的业务方法,但是,我们现在要对这个方法的执行做全面监控,或部分监控.也许我们就会在要一些方法前去加上一条日志记录,
我们写个例子看看我们最简单的解决方案
我们先写一个接口IHello.java代码如下:

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?interface?IHello?反照实现 AOP 动态代理模式{
?4反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
?5反照实现 AOP 动态代理模式?????*?假设这是一个业务方法
?6反照实现 AOP 动态代理模式?????*?@param?name
?7反照实现 AOP 动态代理模式?????*/
?8反照实现 AOP 动态代理模式????void?sayHello(String?name);
?9反照实现 AOP 动态代理模式}
10反照实现 AOP 动态代理模式


里面有个方法,用于输入"Hello" 加传进来的姓名;我们去写个类实现IHello接口

反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Hello?implements?IHello?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?sayHello(String?name)?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式????????System.out.println("Hello?"?+?name);
反照实现 AOP 动态代理模式????}
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式}
反照实现 AOP 动态代理模式


现在我们要为这个业务方法加上日志记录的业务,我们在不改变原代码的情况下,我们会去怎么做呢?也许,你会去写一个类去实现IHello接口,并依赖Hello这个类.代码如下:

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?HelloProxy?implements?IHello?反照实现 AOP 动态代理模式{
?4反照实现 AOP 动态代理模式????private?IHello?hello;
?5反照实现 AOP 动态代理模式
?6反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?HelloProxy(IHello?hello)?反照实现 AOP 动态代理模式{
?7反照实现 AOP 动态代理模式????????this.hello?=?hello;
?8反照实现 AOP 动态代理模式????}
?9反照实现 AOP 动态代理模式
10反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?sayHello(String?name)?反照实现 AOP 动态代理模式{
11反照实现 AOP 动态代理模式????????Logger.logging(Level.DEBUGE,?"sayHello?method?start反照实现 AOP 动态代理模式.");
12反照实现 AOP 动态代理模式????????hello.sayHello(name);
13反照实现 AOP 动态代理模式????????Logger.logging(Level.INFO,?"sayHello?method?end!");
14反照实现 AOP 动态代理模式
15反照实现 AOP 动态代理模式????}
16反照实现 AOP 动态代理模式
17反照实现 AOP 动态代理模式}
18反照实现 AOP 动态代理模式


其中.Logger类和Level枚举代码如下:
Logger.java

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式import?java.util.Date;
?4反照实现 AOP 动态代理模式
?5反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Logger反照实现 AOP 动态代理模式{
?6反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
?7反照实现 AOP 动态代理模式?????*?根据等级记录日志
?8反照实现 AOP 动态代理模式?????*?@param?level
?9反照实现 AOP 动态代理模式?????*?@param?context
10反照实现 AOP 动态代理模式?????*/
11反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?static?void?logging(Level?level,?String?context)?反照实现 AOP 动态代理模式{
12反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????if?(level.equals(Level.INFO))?反照实现 AOP 动态代理模式{
13反照实现 AOP 动态代理模式????????????System.out.println(new?Date().toLocaleString()?+?"?"?+?context);
14反照实现 AOP 动态代理模式????????}
15反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????if?(level.equals(Level.DEBUGE))?反照实现 AOP 动态代理模式{
16反照实现 AOP 动态代理模式????????????System.err.println(new?Date()?+?"?"?+?context);
17反照实现 AOP 动态代理模式????????}
18反照实现 AOP 动态代理模式????}
19反照实现 AOP 动态代理模式
20反照实现 AOP 动态代理模式}
21反照实现 AOP 动态代理模式

Level.java

1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
2反照实现 AOP 动态代理模式
3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?enum?Level?反照实现 AOP 动态代理模式{
4反照实现 AOP 动态代理模式????INFO,DEBUGE;
5反照实现 AOP 动态代理模式}
6反照实现 AOP 动态代理模式

那我们去写个测试类看看,代码如下:
Test.java

1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
2反照实现 AOP 动态代理模式
3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Test?反照实现 AOP 动态代理模式{
4反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?static?void?main(String[]?args)?反照实现 AOP 动态代理模式{
5反照实现 AOP 动态代理模式????????IHello?hello?=?new?HelloProxy(new?Hello());
6反照实现 AOP 动态代理模式????????hello.sayHello("Doublej");
7反照实现 AOP 动态代理模式????}
8反照实现 AOP 动态代理模式}
9反照实现 AOP 动态代理模式

运行以上代码我们可以得到下面结果:

反照实现 AOP 动态代理模式Tue?Mar?04?20:57:12?CST?2008?sayHello?method?start反照实现 AOP 动态代理模式.
反照实现 AOP 动态代理模式Hello?Doublej
反照实现 AOP 动态代理模式2008-3-4?20:57:12?sayHello?method?end!


从上面的代码我们可以看出,hello对象是被HelloProxy这个所谓的代理态所创建的.这样,如果我们以后要把日志记录的功能去掉.那我们只要把得到hello对象的代码改成以下:

1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.staticaop;
2反照实现 AOP 动态代理模式
3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Test?反照实现 AOP 动态代理模式{
4反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?static?void?main(String[]?args)?反照实现 AOP 动态代理模式{
5反照实现 AOP 动态代理模式????????IHello?hello?=?new?Hello();
6反照实现 AOP 动态代理模式????????hello.sayHello("Doublej");
7反照实现 AOP 动态代理模式????}
8反照实现 AOP 动态代理模式}
9反照实现 AOP 动态代理模式


上面代码,可以说是AOP最简单的实现!
但是我们会发现一个问题,如果我们像Hello这样的类很多,那么,我们是不是要去写很多个HelloProxy这样的类呢.没错,是的.其实也是一种很麻烦的事.在jdk1.3以后.jdk跟我们提供了一个API?? java.lang.reflect.InvocationHandler的类. 这个类可以让我们在JVM调用某个类的方法时动态的为些方法做些什么事.让我们把以上的代码改一下来看看效果.
同样,我们写一个IHello的接口和一个Hello的实现类.在接口中.我们定义两个方法;代码如下 :

IHello.java

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?interface?IHello?反照实现 AOP 动态代理模式{
?4反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
?5反照实现 AOP 动态代理模式?????*?业务处理A方法
?6反照实现 AOP 动态代理模式?????*?@param?name
?7反照实现 AOP 动态代理模式?????*/
?8反照实现 AOP 动态代理模式????void?sayHello(String?name);
?9反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
10反照实现 AOP 动态代理模式?????*?业务处理B方法
11反照实现 AOP 动态代理模式?????*?@param?name
12反照实现 AOP 动态代理模式?????*/
13反照实现 AOP 动态代理模式????void?sayGoogBye(String?name);
14反照实现 AOP 动态代理模式}
15反照实现 AOP 动态代理模式



Hello.java

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Hello?implements?IHello?反照实现 AOP 动态代理模式{
?4反照实现 AOP 动态代理模式
?5反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?sayHello(String?name)?反照实现 AOP 动态代理模式{
?6反照实现 AOP 动态代理模式????????System.out.println("Hello?"?+?name);
?7反照实现 AOP 动态代理模式????}
?8反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?sayGoogBye(String?name)?反照实现 AOP 动态代理模式{
?9反照实现 AOP 动态代理模式????????System.out.println(name+"?GoodBye!");
10反照实现 AOP 动态代理模式????}
11反照实现 AOP 动态代理模式}
12反照实现 AOP 动态代理模式


我们一样的去写一个代理类.只不过.让这个类去实现java.lang.reflect.InvocationHandler接口,代码如下:

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式import?java.lang.reflect.InvocationHandler;
?4反照实现 AOP 动态代理模式import?java.lang.reflect.Method;
?5反照实现 AOP 动态代理模式import?java.lang.reflect.Proxy;
?6反照实现 AOP 动态代理模式
?7反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?DynaProxyHello?implements?InvocationHandler?反照实现 AOP 动态代理模式{
?8反照实现 AOP 动态代理模式
?9反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
10反照实现 AOP 动态代理模式?????*?要处理的对象(也就是我们要在方法的前后加上业务逻辑的对象,如例子中的Hello)
11反照实现 AOP 动态代理模式?????*/
12反照实现 AOP 动态代理模式????private?Object?delegate;
13反照实现 AOP 动态代理模式
14反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
15反照实现 AOP 动态代理模式?????*?动态生成方法被处理过后的对象?(写法固定)
16反照实现 AOP 动态代理模式?????*?
17反照实现 AOP 动态代理模式?????*?@param?delegate
18反照实现 AOP 动态代理模式?????*?@param?proxy
19反照实现 AOP 动态代理模式?????*?@return
20反照实现 AOP 动态代理模式?????*/
21反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?Object?bind(Object?delegate)?反照实现 AOP 动态代理模式{
22反照实现 AOP 动态代理模式????????this.delegate?=?delegate;
23反照实现 AOP 动态代理模式????????return?Proxy.newProxyInstance(
24反照实现 AOP 动态代理模式????????????????this.delegate.getClass().getClassLoader(),?this.delegate
25反照实现 AOP 动态代理模式????????????????????????.getClass().getInterfaces(),?this);
26反照实现 AOP 动态代理模式????}
27反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
28反照实现 AOP 动态代理模式?????*?要处理的对象中的每个方法会被此方法送去JVM调用,也就是说,要处理的对象的方法只能通过此方法调用
29反照实现 AOP 动态代理模式?????*?此方法是动态的,不是手动调用的
30反照实现 AOP 动态代理模式?????*/
31反照实现 AOP 动态代理模式????public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)
32反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????????throws?Throwable?反照实现 AOP 动态代理模式{
33反照实现 AOP 动态代理模式????????Object?result?=?null;
34反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????try?反照实现 AOP 动态代理模式{
35反照实现 AOP 动态代理模式????????????//执行原来的方法之前记录日志
36反照实现 AOP 动态代理模式????????????Logger.logging(Level.DEBUGE,?method.getName()?+?"?Method?end?反照实现 AOP 动态代理模式.");
37反照实现 AOP 动态代理模式????????????
38反照实现 AOP 动态代理模式????????????//JVM通过这条语句执行原来的方法(反射机制)
39反照实现 AOP 动态代理模式????????????result?=?method.invoke(this.delegate,?args);
40反照实现 AOP 动态代理模式????????????//执行原来的方法之后记录日志
41反照实现 AOP 动态代理模式????????????Logger.logging(Level.INFO,?method.getName()?+?"?Method?Start!");
42反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????}?catch?(Exception?e)?反照实现 AOP 动态代理模式{
43反照实现 AOP 动态代理模式????????????e.printStackTrace();
44反照实现 AOP 动态代理模式????????}
45反照实现 AOP 动态代理模式????????//返回方法返回值给调用者
46反照实现 AOP 动态代理模式????????return?result;
47反照实现 AOP 动态代理模式????}
48反照实现 AOP 动态代理模式
49反照实现 AOP 动态代理模式}
50反照实现 AOP 动态代理模式


上面类中出现的Logger类和Level枚举还是和上一上例子的实现是一样的.这里就不贴出代码了.

让我们写一个Test类去测试一下.代码如下:
Test.java

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Test?反照实现 AOP 动态代理模式{
?4反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?static?void?main(String[]?args)?反照实现 AOP 动态代理模式{
?5反照实现 AOP 动态代理模式????????IHello?hello?=?(IHello)new?DynaProxyHello().bind(new?Hello());
?6反照实现 AOP 动态代理模式????????hello.sayGoogBye("Double?J");
?7反照实现 AOP 动态代理模式????????hello.sayHello("Double?J");
?8反照实现 AOP 动态代理模式????????
?9反照实现 AOP 动态代理模式????}
10反照实现 AOP 动态代理模式}
11反照实现 AOP 动态代理模式


运行输出的结果如下:

反照实现 AOP 动态代理模式Tue?Mar?04?21:24:03?CST?2008?sayGoogBye?Method?end?反照实现 AOP 动态代理模式.
反照实现 AOP 动态代理模式Double?J?GoodBye!
反照实现 AOP 动态代理模式2008-3-4?21:24:03?sayGoogBye?Method?Start!
反照实现 AOP 动态代理模式Tue?Mar?04?21:24:03?CST?2008?sayHello?Method?end?反照实现 AOP 动态代理模式.
反照实现 AOP 动态代理模式Hello?Double?J
反照实现 AOP 动态代理模式2008-3-4?21:24:03?sayHello?Method?Start!


由于线程的关系,第二个方法的开始出现在第一个方法的结束之前.这不是我们所关注的!
从上面的例子我们看出.只要你是采用面向接口编程,那么,你的任何对象的方法执行之前要加上记录日志的操作都是可以的.他(DynaPoxyHello)自动去代理执行被代理对象(Hello)中的每一个方法,一个java.lang.reflect.InvocationHandler接口就把我们的代理对象和被代理对象解藕了.但是,我们又发现还有一个问题,这个DynaPoxyHello对象只能跟我们去在方法前后加上日志记录的操作.我们能不能把DynaPoxyHello对象和日志操作对象(Logger)解藕呢?
结果是肯定的.让我们来分析一下我们的需求.
我们要在被代理对象的方法前面或者后面去加上日志操作代码(或者是其它操作的代码),
那么,我们可以抽象出一个接口,这个接口里就只有两个方法,一个是在被代理对象要执行方法之前执行的方法,我们取名为start,第二个方法就是在被代理对象执行方法之后执行的方法,我们取名为end .接口定义如下 :

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式import?java.lang.reflect.Method;
?4反照实现 AOP 动态代理模式
?5反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?interface?IOperation?反照实现 AOP 动态代理模式{
?6反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
?7反照实现 AOP 动态代理模式?????*?方法执行之前的操作
?8反照实现 AOP 动态代理模式?????*?@param?method
?9反照实现 AOP 动态代理模式?????*/
10反照实现 AOP 动态代理模式????void?start(Method?method);
11反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
12反照实现 AOP 动态代理模式?????*?方法执行之后的操作
13反照实现 AOP 动态代理模式?????*?@param?method
14反照实现 AOP 动态代理模式?????*/
15反照实现 AOP 动态代理模式????void?end(Method?method);
16反照实现 AOP 动态代理模式}
17反照实现 AOP 动态代理模式


我们去写一个实现上面接口的类.我们把作他真正的操作者,如下面是日志操作者的一个类:
LoggerOperation.java

反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式import?java.lang.reflect.Method;
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?LoggerOperation?implements?IOperation?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?end(Method?method)?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式????????Logger.logging(Level.DEBUGE,?method.getName()?+?"?Method?end?反照实现 AOP 动态代理模式.");
反照实现 AOP 动态代理模式????}
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?start(Method?method)?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式????????Logger.logging(Level.INFO,?method.getName()?+?"?Method?Start!");
反照实现 AOP 动态代理模式????}
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式}
反照实现 AOP 动态代理模式


然后我们要改一下代理对象DynaProxyHello中的代码.如下:

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式import?java.lang.reflect.InvocationHandler;
?4反照实现 AOP 动态代理模式import?java.lang.reflect.Method;
?5反照实现 AOP 动态代理模式import?java.lang.reflect.Proxy;
?6反照实现 AOP 动态代理模式
?7反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?DynaProxyHello?implements?InvocationHandler?反照实现 AOP 动态代理模式{
?8反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
?9反照实现 AOP 动态代理模式?????*?操作者
10反照实现 AOP 动态代理模式?????*/
11反照实现 AOP 动态代理模式????private?Object?proxy;
12反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
13反照实现 AOP 动态代理模式?????*?要处理的对象(也就是我们要在方法的前后加上业务逻辑的对象,如例子中的Hello)
14反照实现 AOP 动态代理模式?????*/
15反照实现 AOP 动态代理模式????private?Object?delegate;
16反照实现 AOP 动态代理模式
17反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
18反照实现 AOP 动态代理模式?????*?动态生成方法被处理过后的对象?(写法固定)
19反照实现 AOP 动态代理模式?????*?
20反照实现 AOP 动态代理模式?????*?@param?delegate
21反照实现 AOP 动态代理模式?????*?@param?proxy
22反照实现 AOP 动态代理模式?????*?@return
23反照实现 AOP 动态代理模式?????*/
24反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?Object?bind(Object?delegate,Object?proxy)?反照实现 AOP 动态代理模式{
25反照实现 AOP 动态代理模式????????
26反照实现 AOP 动态代理模式????????this.proxy?=?proxy;
27反照实现 AOP 动态代理模式????????this.delegate?=?delegate;
28反照实现 AOP 动态代理模式????????return?Proxy.newProxyInstance(
29反照实现 AOP 动态代理模式????????????????this.delegate.getClass().getClassLoader(),?this.delegate
30反照实现 AOP 动态代理模式????????????????????????.getClass().getInterfaces(),?this);
31反照实现 AOP 动态代理模式????}
32反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????/**?*//**
33反照实现 AOP 动态代理模式?????*?要处理的对象中的每个方法会被此方法送去JVM调用,也就是说,要处理的对象的方法只能通过此方法调用
34反照实现 AOP 动态代理模式?????*?此方法是动态的,不是手动调用的
35反照实现 AOP 动态代理模式?????*/
36反照实现 AOP 动态代理模式????public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)
37反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????????throws?Throwable?反照实现 AOP 动态代理模式{
38反照实现 AOP 动态代理模式????????Object?result?=?null;
39反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????try?反照实现 AOP 动态代理模式{
40反照实现 AOP 动态代理模式????????????//反射得到操作者的实例
41反照实现 AOP 动态代理模式????????????Class?clazz?=?this.proxy.getClass();
42反照实现 AOP 动态代理模式????????????//反射得到操作者的Start方法
43反照实现 AOP 动态代理模式????????????Method?start?=?clazz.getDeclaredMethod("start",
44反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????????????????new?Class[]?反照实现 AOP 动态代理模式{?Method.class?});
45反照实现 AOP 动态代理模式????????????//反射执行start方法
46反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????????start.invoke(this.proxy,?new?Object[]?反照实现 AOP 动态代理模式{?method?});
47反照实现 AOP 动态代理模式????????????//执行要处理对象的原本方法
48反照实现 AOP 动态代理模式????????????result?=?method.invoke(this.delegate,?args);
49反照实现 AOP 动态代理模式//????????????反射得到操作者的end方法
50反照实现 AOP 动态代理模式????????????Method?end?=?clazz.getDeclaredMethod("end",
51反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????????????????new?Class[]?反照实现 AOP 动态代理模式{?Method.class?});
52反照实现 AOP 动态代理模式//????????????反射执行end方法
53反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????????end.invoke(this.proxy,?new?Object[]?反照实现 AOP 动态代理模式{?method?});
54反照实现 AOP 动态代理模式
55反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????????}?catch?(Exception?e)?反照实现 AOP 动态代理模式{
56反照实现 AOP 动态代理模式????????????e.printStackTrace();
57反照实现 AOP 动态代理模式????????}
58反照实现 AOP 动态代理模式????????return?result;
59反照实现 AOP 动态代理模式????}
60反照实现 AOP 动态代理模式
61反照实现 AOP 动态代理模式}
62反照实现 AOP 动态代理模式


然后我们把Test.java中的代码改一下.测试一下:

反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
反照实现 AOP 动态代理模式
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?Test?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?static?void?main(String[]?args)?反照实现 AOP 动态代理模式{
反照实现 AOP 动态代理模式????????IHello?hello?=?(IHello)new?DynaProxyHello().bind(new?Hello(),new?LoggerOperation());
反照实现 AOP 动态代理模式????????hello.sayGoogBye("Double?J");
反照实现 AOP 动态代理模式????????hello.sayHello("Double?J");
反照实现 AOP 动态代理模式????????
反照实现 AOP 动态代理模式????}
反照实现 AOP 动态代理模式}
反照实现 AOP 动态代理模式

结果还是一样的吧.

如果你想在每个方法之前加上日志记录,而不在方法后加上日志记录.你就把LoggerOperation类改成如下:

?1反照实现 AOP 动态代理模式package?sinosoft.dj.aop.proxyaop;
?2反照实现 AOP 动态代理模式
?3反照实现 AOP 动态代理模式import?java.lang.reflect.Method;
?4反照实现 AOP 动态代理模式
?5反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式public?class?LoggerOperation?implements?IOperation?反照实现 AOP 动态代理模式{
?6反照实现 AOP 动态代理模式
?7反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?end(Method?method)?反照实现 AOP 动态代理模式{
?8反照实现 AOP 动态代理模式????????//Logger.logging(Level.DEBUGE,?method.getName()?+?"?Method?end?反照实现 AOP 动态代理模式.");
?9反照实现 AOP 动态代理模式????}
10反照实现 AOP 动态代理模式
11反照实现 AOP 动态代理模式反照实现 AOP 动态代理模式????public?void?start(Method?method)?反照实现 AOP 动态代理模式{
12反照实现 AOP 动态代理模式????????Logger.logging(Level.INFO,?method.getName()?+?"?Method?Start!");
13反照实现 AOP 动态代理模式????}
14反照实现 AOP 动态代理模式
15反照实现 AOP 动态代理模式}
16反照实现 AOP 动态代理模式


运行一下.你就会发现,每个方法之后没有记录日志了. 这样,我们就把代理者和操作者解藕了!

下面留一个问题给大家,如果我们不想让所有方法都被日志记录,我们应该怎么去解藕呢.?
我的想法是在代理对象的public Object invoke(Object proxy, Method method, Object[] args)方法里面加上个if(),对传进来的method的名字进行判断,判断的条件存在XML里面.这样我们就可以配置文件时行解藕了.如果有兴趣的朋友可以把操作者,被代理者,都通过配置进行配置 ,那么就可以写一个简单的SpringAOP框架了.

热点排行