首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

AOP简略分析

2012-08-02 
AOP简单分析?AOP的意思就是面向切面编程.OO注重的是我们解决问题的方法(封装成Method),而AOP注重的是许多

AOP简单分析

?

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

?

?1AOP简略分析package?sinosoft.dj.aop.staticaop;
?2AOP简略分析
?3AOP简略分析public?interface?IHello?{
?4AOP简略分析????/**
?5AOP简略分析?????*?假设这是一个业务方法
?6AOP简略分析?????*?@param?name
?7AOP简略分析?????*/
?8AOP简略分析????void?sayHello(String?name);
?9AOP简略分析}
10AOP简略分析

?

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

AOP简略分析package?sinosoft.dj.aop.staticaop;
AOP简略分析
AOP简略分析public?class?Hello?implements?IHello?{
AOP简略分析
AOP简略分析????public?void?sayHello(String?name)?{
AOP简略分析????????System.out.println("Hello?"?+?name);
AOP简略分析????}
AOP简略分析
AOP简略分析}
AOP简略分析

?

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

?1AOP简略分析package?sinosoft.dj.aop.staticaop;
?2AOP简略分析
?3AOP简略分析public?class?HelloProxy?implements?IHello?{
?4AOP简略分析????private?IHello?hello;
?5AOP简略分析
?6AOP简略分析????public?HelloProxy(IHello?hello)?{
?7AOP简略分析????????this.hello?=?hello;
?8AOP简略分析????}
?9AOP简略分析
10AOP简略分析????public?void?sayHello(String?name)?{
11AOP简略分析????????Logger.logging(Level.DEBUGE,?"sayHello?method?startAOP简略分析.");
12AOP简略分析????????hello.sayHello(name);
13AOP简略分析????????Logger.logging(Level.INFO,?"sayHello?method?end!");
14AOP简略分析
15AOP简略分析????}
16AOP简略分析
17AOP简略分析}
18AOP简略分析

?

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

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

?

Level.java

1AOP简略分析package?sinosoft.dj.aop.staticaop;
2AOP简略分析
3AOP简略分析public?enum?Level?{
4AOP简略分析????INFO,DEBUGE;
5AOP简略分析}
6AOP简略分析

?

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

1AOP简略分析package?sinosoft.dj.aop.staticaop;
2AOP简略分析
3AOP简略分析public?class?Test?{
4AOP简略分析????public?static?void?main(String[]?args)?{
5AOP简略分析????????IHello?hello?=?new?HelloProxy(new?Hello());
6AOP简略分析????????hello.sayHello("Doublej");
7AOP简略分析????}
8AOP简略分析}
9AOP简略分析

?

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

AOP简略分析Tue?Mar?04?20:57:12?CST?2008?sayHello?method?startAOP简略分析.
AOP简略分析Hello?Doublej
AOP简略分析2008-3-4?20:57:12?sayHello?method?end!


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

1AOP简略分析package?sinosoft.dj.aop.staticaop;
2AOP简略分析
3AOP简略分析public?class?Test?{
4AOP简略分析????public?static?void?main(String[]?args)?{
5AOP简略分析????????IHello?hello?=?new?Hello();
6AOP简略分析????????hello.sayHello("Doublej");
7AOP简略分析????}
8AOP简略分析}
9AOP简略分析

?

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

IHello.java

?1AOP简略分析package?sinosoft.dj.aop.proxyaop;
?2AOP简略分析
?3AOP简略分析public?interface?IHello?{
?4AOP简略分析????/**
?5AOP简略分析?????*?业务处理A方法
?6AOP简略分析?????*?@param?name
?7AOP简略分析?????*/
?8AOP简略分析????void?sayHello(String?name);
?9AOP简略分析????/**
10AOP简略分析?????*?业务处理B方法
11AOP简略分析?????*?@param?name
12AOP简略分析?????*/
13AOP简略分析????void?sayGoogBye(String?name);
14AOP简略分析}
15AOP简略分析



Hello.java?

?1AOP简略分析package?sinosoft.dj.aop.proxyaop;
?2AOP简略分析
?3AOP简略分析public?class?Hello?implements?IHello?{
?4AOP简略分析
?5AOP简略分析????public?void?sayHello(String?name)?{
?6AOP简略分析????????System.out.println("Hello?"?+?name);
?7AOP简略分析????}
?8AOP简略分析????public?void?sayGoogBye(String?name)?{
?9AOP简略分析????????System.out.println(name+"?GoodBye!");
10AOP简略分析????}
11AOP简略分析}
12AOP简略分析

?

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

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

?

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

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

?1AOP简略分析package?sinosoft.dj.aop.proxyaop;
?2AOP简略分析
?3AOP简略分析public?class?Test?{
?4AOP简略分析????public?static?void?main(String[]?args)?{
?5AOP简略分析????????IHello?hello?=?(IHello)new?DynaProxyHello().bind(new?Hello());
?6AOP简略分析????????hello.sayGoogBye("Double?J");
?7AOP简略分析????????hello.sayHello("Double?J");
?8AOP简略分析????????
?9AOP简略分析????}
10AOP简略分析}
11AOP简略分析

?

运行输出的结果如下:

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 .接口定义如下 :

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

?

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

AOP简略分析package?sinosoft.dj.aop.proxyaop;
AOP简略分析
AOP简略分析import?java.lang.reflect.Method;
AOP简略分析
AOP简略分析public?class?LoggerOperation?implements?IOperation?{
AOP简略分析
AOP简略分析????public?void?end(Method?method)?{
AOP简略分析????????Logger.logging(Level.DEBUGE,?method.getName()?+?"?Method?end?AOP简略分析.");
AOP简略分析????}
AOP简略分析
AOP简略分析????public?void?start(Method?method)?{
AOP简略分析????????Logger.logging(Level.INFO,?method.getName()?+?"?Method?Start!");
AOP简略分析????}
AOP简略分析
AOP简略分析}
AOP简略分析

?

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

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


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

AOP简略分析package?sinosoft.dj.aop.proxyaop;
AOP简略分析
AOP简略分析public?class?Test?{
AOP简略分析????public?static?void?main(String[]?args)?{
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类改成如下:

?1AOP简略分析package?sinosoft.dj.aop.proxyaop;
?2AOP简略分析
?3AOP简略分析import?java.lang.reflect.Method;
?4AOP简略分析
?5AOP简略分析public?class?LoggerOperation?implements?IOperation?{
?6AOP简略分析
?7AOP简略分析????public?void?end(Method?method)?{
?8AOP简略分析????????//Logger.logging(Level.DEBUGE,?method.getName()?+?"?Method?end?AOP简略分析.");
?9AOP简略分析????}
10AOP简略分析
11AOP简略分析????public?void?start(Method?method)?{
12AOP简略分析????????Logger.logging(Level.INFO,?method.getName()?+?"?Method?Start!");
13AOP简略分析????}
14AOP简略分析
15AOP简略分析}
16AOP简略分析

?

?

?

?

原文:http://www.blogjava.net/DoubleJ/archive/2008/03/04/183796.html

?

?

?

?

?

热点排行