我是靠谱客的博主 动听西装,这篇文章主要介绍如何在不使用spring框架中使用aop的功能,现在分享给大家,希望可以做个参考。

Spring框架的AOP机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代码。在业务流程执行过程中,Spring框架会根据业务流程要求,自动把独立编写的功能代码切入到流程的合适位置。

spring提供了两种方式的AOP使用

使用xml配置方式

使用注解方式

这里需要注意的是Spring AOP目前仅仅支持方法级别的切面,成员的interception并没有实现。另外,spring aop仅仅是集成框架,并没有参与aop的具体开发。

如果想利用aop的更多功能,或者在不使用spring的框架中使用aop的功能,该怎么办呢?

AspectJ简介

spring aop集成了AspectJ(可以和java编程语言无缝结合的一个面向切面编程的可扩展框架)

AspectJ的使用实例

Eclipse Marketplace安装插件AJDT

创建Aspect工程

创建AspectJ测试类

创建一个切面Aspect文件

.aj文件

运行HelloAspectJDemo的java程序,结果为:

不使用spring的aop功能实现日志输出

第一种

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class TimeBook {undefined  private Logger logger = Logger.getLogger(this.getClass().getName());  //审核数据的相关程序  public void doAuditing(String name){undefined   logger.log(Level.INFO, name + "开始审核数据...");   System.out.println("审核程序");   logger.log(Level.INFO, name + "审核数据结束...");  } } //TestHelloWorld.java package com.gc.test; import com.gc.action.TimeBook; public class TestHelloWorld {undefined  public static void main(String[] args){undefined   TimeBook timeBook = new TimeBook();   timeBook.doAuditing("张三");  } }

第二种:通过面向接口编程实现日志输出

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class TimeBook implements TimeBookInterface {undefined  //审核数据的相关程序  public void doAuditing(String name){undefined   System.out.println("审核程序");  } } //TimeBookProxy.java package com.gc.action; import org.apache.log4j.Level; import org.apache.log4j.Logger; import com.gc.impl.TimeBookInterface; public class TimeBookProxy {undefined  private Logger logger = Logger.getLogger(this.getClass().getName());  private TimeBookInterface timeBookInterface;  //在该类中针对前面的接口TimeBookInterface编程,而不是针对具体的类  public TimeBookProxy(TimeBookInterface timeBookInterface){undefined   this.timeBookInterface = timeBookInterface;  }  //实际业务处理  public void doAuditing(String name){undefined   logger.log(Level.INFO,"开始审核数据 "+name);   timeBookInterface.doAuditing(name);   logger.log(Level.INFO,"审核数据结束 "+name);  } } public class TestHelloWorld {undefined  public static void main(String[] args){undefined   TimeBookProxy timeBookProxy = new TimeBookProxy(new TimeBook());   timeBookProxy.doAuditing("张三");  } }

第三种:使用java的代理机制进行日志输出

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class LogProxy implements InvocationHandler{undefined  private Logger logger = Logger.getLogger(this.getClass().getName());  private Object delegate;  //绑定代理对象  public Object bind(Object delegate){undefined   this.delegate = delegate;   return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),     delegate.getClass().getInterfaces(),this);  }  //针对接口编程  public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {undefined   Object result = null;   try{undefined    //在方法调用前后进行日志输出    logger.log(Level.INFO,args[0]+" 开始审核数据...");    result = method.invoke(delegate, args);    logger.log(Level.INFO,args[0]+" 审核数据结束...");   }catch(Exception e){undefined    logger.log(Level.INFO,e.toString());   }   return result;  } } //TimeBookInterface.java package com.gc.impl; //针对接口编程 public interface TimeBookInterface {undefined  public void doAuditing(String name); } //TimeBook.java public class TimeBook implements TimeBookInterface {undefined  //审核数据的相关程序  public void doAuditing(String name){undefined   System.out.println("审核程序");  } } //TestHelloWorld.java public class TestHelloWorld {undefined  public static void main(String[] args){undefined   //实现了对日志类的重用   LogProxy logProxy = new LogProxy();   TimeBookInterface timeBookProxy = (TimeBookInterface)logProxy.bind(new TimeBook());   timeBookProxy.doAuditing("张三");  } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是动听西装最近收集整理的关于如何在不使用spring框架中使用aop的功能的全部内容,更多相关如何在不使用spring框架中使用aop内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(191)

评论列表共有 0 条评论

立即
投稿
返回
顶部