经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
如何在不使用spring框架中使用aop的功能
来源:jb51  时间:2022/1/3 14:29:39  对本文有异议

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

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

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

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

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号