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

反照实现 AOP 动态代理模式(1)

2012-10-06 
反射实现 AOP 动态代理模式(1)其实AOP的意思就是面向切面编程.OO注重的是我们解决问题的方法(封装成Method

反射实现 AOP 动态代理模式(1)

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

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


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

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


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

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


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

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

Level.java

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

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

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

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

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


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

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


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

IHello.java

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



Hello.java

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


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

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


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

热点排行