Spring AOP官方文档学习笔记(四)之Spring AOP的其他知识点 ...

打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

1.选择哪种AOP
(1) 使用Spring AOP比使用完整版的AspectJ更方便简单,因为不需要在开发和构建过程中引入AspectJ编译器以及织入器,如果我们只希望通知能够在Spring Bean上执行,那么选用Spring AOP就可以了,如果我们希望通知能够在不由Spring所管理的对象上执行,那么就需要使用AspectJ,如果我们希望为除方法以外的连接点(比如成员变量)提供通知,那么也需要使用AspectJ
2.Spring AOP的代理机制
(1) Spring AOP使用Jdk动态代理或Cglib动态代理来为目标对象创建代理对象,Jdk动态代理由Jdk提供,而Cglib动态代理则是由一个开源类库提供,如果要代理的目标对象至少实现了一个接口,那么就会使用Jdk动态代理,否则如果目标对象没有实现任何接口,那么就会使用Cglib动态代理,创建一个Cglib代理对象
(2) Spring默认使用Jdk动态代理,但我们可以强制让Spring始终使用Cglib动态代理,但需注意,使用Cglib动态代理,无法对final修饰的方法织入通知,因为这些方法不能在子类中被重写,具体开启Cglib动态代理的方式如下
  1. <aop:config proxy-target->
  2.         
  3. </aop:config>
  4. <aop:aspectj-autoproxy proxy-target-/>
  5. @EnableAspectJAutoProxy(proxyTargetClass = true)
复制代码
(3) 当一个bean被代理后,我们从容器中获取到这个bean,并对其使用 .getClass().getName() 方法来输出它的类名称,可见如 cn.example.spring.boke.ExampleA$$EnhancerBySpringCGLIB$$ff6c22d2或com.sun.proxy.$Proxy18 这样的输出,而当我们关闭掉AOP后,得到的通常是形如 cn.example.spring.boke.ExampleA 这样的输出,这其实是因为我们从容器中获取的是该bean被增强过后的代理对象,而非它原始的目标对象,因而,对这个bean的方法调用就是对代理对象的方法调用,然后由代理对象委托调用原始对象上相关的方法以及该方法相关的拦截器(advice),如下
[img=60%,60%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509121521937-1372084513.png[/img](4) 在目标对象中,使用this指针进行自调用不会触发通知的执行
  1. //一个普通的bean,在它的a方法中使用this指针,自调用b方法
  2. @Component
  3. public class ExampleA{
  4.     public void a() {
  5.         System.out.println("a...");
  6.         this.b();
  7.     }
  8.     public void b() {
  9.         System.out.println("b...");
  10.     }
  11. }
  12. //切面,切入ExampleA这个bean中的所有方法
  13. @Component
  14. @Aspect
  15. public class Logger {
  16.     @Before(value = "execution(* cn.example.spring.boke.ExampleA.*(..))")
  17.     public void beforePrint() {
  18.         System.out.println("beforePrint...");
  19.     }
  20. }
  21. @Configuration
  22. @EnableAspectJAutoProxy
  23. @ComponentScan(basePackages = "cn.example.spring.boke")
  24. public class Config { }
  25. //获取ExampleA,调用a方法,打印结果
  26. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
  27. ExampleA exampleA = ctx.getBean(ExampleA.class);
  28. exampleA.a();
  29. //可见,Spring对a方法进行了织入,而b方法却没有,原因就是因为这里的this指向的是目标对象,一个普通的bean ExampleA,而非它的代理对象,自然而然无法进行织入了,因此关键的目标就是如何获取到代理对象
  30. beforePrint...
  31. a...
  32. b...
  33. //想要获取代理对象,首先要先将@EnableAspectJAutoProxy注解中的exposeProxy属性设置为true
  34. @EnableAspectJAutoProxy(exposeProxy = true)
  35. //接着修改ExampleA中的a方法,在调用b方法时不再使用this指针,而是AopContext.currentProxy(),即获取当前对象的代理对象
  36. public void a() {
  37.     System.out.println("a...");
  38. //        this.b();
  39.     ((ExampleA) AopContext.currentProxy()).b();
  40. }
  41. //接着,再执行打印,可见此时通知已被正确执行
  42. beforePrint...
  43. a...
  44. beforePrint...
  45. b...
复制代码
Spring不推荐使用如上的方法,因为这会使Spring AOP与我们的代码强耦合,具有侵入性,最好的方式是重构我们的代码,避免发生自调用,此外,Spring AOP会产生这种问题的原因是Spring AOP是基于代理实现的,而AspectJ框架就不存在这种自调用问题,因为它不是一个基于代理的AOP框架
3.基于java代码的AOP
  1. public class ExampleA{
  2.     public void a() {
  3.         System.out.println("a...");
  4.     }
  5. }
  6. //切面必须由@Aspect注解标注,否则容器会抛出异常
  7. @Aspect
  8. public class Logger {
  9.     @Before(value = "execution(* cn.example.spring.boke.ExampleA.*(..))")
  10.     public void beforePrint() {
  11.         System.out.println("beforePrint...");
  12.     }
  13. }
  14. //创建AOP工厂,生成代理对象
  15. public static void main(String[] args) throws Exception {
  16.     //1.使用AspectJProxyFactory工厂,用于生成目标对象的代理对象
  17.     AspectJProxyFactory factory = new AspectJProxyFactory(new ExampleA());
  18.     //2.添加一个切面,该切面必须由@Aspect注解标注
  19.     factory.addAspect(Logger.class);
  20.     //3.生成代理对象
  21.     ExampleA proxy = factory.getProxy();
  22.     proxy.a();
  23. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表