Spring AOP为什么会失效

发布时间:2022-01-14 09:50:56 作者:柒染
来源:亿速云 阅读:108

今天给大家介绍一下Spring AOP为什么会失效。文章的内容小编觉得不错,现在给大家分享一下,觉得有需要的朋友可以了解一下,希望对大家有所帮助,下面跟着小编的思路一起来阅读吧。

怎么理解AOP代理。

Spring AOP是基于代理的。

在编写自己的方面或使用Spring Framework提供的任何基于Spring AOP的方面之前,掌握最后一条语句的实际含义是至关重要的。

首先考虑这样一个场景,其中您有一个普通的、未代理的、没有什么特别的、直接的对象引用,如下面的代码片段所示。

public class SimplePojo implements Pojo {  
public void foo() {      
// this next method invocation is a direct call on the
'this' reference  this.bar();   }     public void bar() {      // some logic...   } }

如果在对象引用上调用方法,则会直接在该对象引用上调用该方法,如下所示。

Spring AOP为什么会失效

public class Main {   
   public static void main(String[] args) {      Pojo pojo = new SimplePojo();       // this is a direct method call on the 'pojo' reference  pojo.foo();   } }

当客户端代码拥有的引用是代理时,情况略有变化。

请考虑以下图表和代码片段。

Spring AOP为什么会失效

public class Main {   

   public static void main(String[] args) {
ProxyFactory factory = new ProxyFactory(new
SimplePojo());      factory.addInterface(Pojo.class);      factory.addAdvice(new RetryAdvice());      Pojo pojo = (Pojo) factory.getProxy();            // this is a method call on the proxy!  pojo.foo();   } }

这里要理解的关键是main(..)中的客户端代码。

主类的具有对代理的引用。

这意味着对该对象引用的方法调用将是对代理的调用,因此代理将能够委托给与该特定方法调用相关的所有拦截器(通知)。

然而,一旦调用最终到达目标对象(本例中为SimplePojo引用),它可能对自身进行的任何方法调用(如this.bar()或this.foo())都将针对this引用而不是代理调用。

这具有重要的意义。

这意味着自调用不会导致与方法调用相关联的通知有机会执行。

好的,那么我们该怎么做呢?

最好的方法(这里松散地使用术语“最佳”)是重构您的代码,这样就不会发生自调用。

当然,这确实需要您做一些工作,但这是最好的、侵入性最小的方法。

下一种方法绝对是可怕的,我几乎不愿指出这一点,正是因为它太可怕了。

你可以(窒息!)。

通过执行以下操作,将类中的逻辑完全绑定到Spring AOP:

public class SimplePojo implements Pojo {   

 public void foo() {    
     // this works, but... gah!  ((Pojo) AopContext.currentProxy()).bar();   }     public void bar() {    
      // some logic...   } }

这完全将您的代码耦合到Spring AOP,并且它使类本身意识到它是在AOP上下文中使用的,这与AOP背道而驰。

创建代理时还需要一些额外的配置:

public class Main {  

  public static void main(String[] args) {          ProxyFactory factory = new ProxyFactory
(new SimplePojo());      factory.adddInterface(Pojo.class);      factory.addAdvice(new RetryAdvice());      factory.setExposeProxy(true);      Pojo pojo = (Pojo) factory.getProxy();

     // this is a method call on the proxy!  pojo.foo();   } }

最后,必须注意的是,AspectJ没有这个自调用问题,因为它不是一个基于代理的AOP框架。

 Programmatic creation of @AspectJ Proxies

除了使用<aop:config>或<aop:aspectJ-autoproxy>在配置中声明方面之外,还可以通过编程方式创建向目标对象提供建议的代理。

有关Spring的AOP API的完整细节,请参阅下一章。

在这里,我们想重点介绍使用@AspectJ方面自动创建代理的能力。

类org.springframework.aop.aspectj.annotation.AspectJProxyFactory可用于创建由一个或多个@AspectJ方面建议的目标对象的代理。

这个类的基本用法非常简单,如下所示。

有关完整信息,请参阅Javadoc。

// create a factory that can generate a proxy for the given target object

AspectJProxyFactory factory = new AspectJProxyFactory(targetObject); 


// add an aspect, the class must be an @AspectJ aspect

// you can call this as many times as you need with different aspects

factory.addAspect(SecurityManager.class);


// you can also add existing aspect instances, the type of the object supplied must be an @AspectJ aspect

factory.addAspect(usageTracker);


// now get the proxy object...

MyInterfaceType proxy = factory.getProxy();

在Spring应用程序中使用AspectJ。

到目前为止,我们在本章中介绍的所有内容都是纯Spring AOP。

在这一节中,如果您的需求超出了Spring AOP单独提供的功能,我们将看看如何使用AspectJ编译器/编织器来替代Spring AOP,或者除了Spring AOP之外使用AspectJ编译器/编织器。

Spring附带了一个小的AspectJ方面库,它在您的发行版中作为Spring-aspects.jar独立提供;您需要将其添加到类路径中才能使用其中的方面。

以上就是Spring AOP为什么会失效的全部内容了,更多与Spring AOP为什么会失效相关的内容可以搜索亿速云之前的文章或者浏览下面的文章进行学习哈!相信小编会给大家增添更多知识,希望大家能够支持一下亿速云!

推荐阅读:
  1. Spring常见面试题和解题思路
  2. 如何解决Spring aop失效问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring aop

上一篇:ServerSuperIO怎么集成OPC Client使用

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》