【问题标题】:Spring boot AOP Aspect not being invoked未调用 Spring Boot AOP 方面
【发布时间】:2017-12-26 00:08:08
【问题描述】:

我正在尝试通过以下方式拦截带有方面的休息服务调用

 package mypackage.services.Service;

 @Component
 public class Service {

      @Override
      public Response helloService() {
        return handleResult("Hello test " + new Date());
      }
 }

@Component
@Aspect
public class AuditLog {

     @Before("execution(* mypackage.services.Service.*(..))")
     public void beforeServcie(JoinPoint jp){
       log.info("Before ",jp.getSignature().getName());
     }
}

我正在使用以下 maven 依赖项

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>

这个maven插件

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.0</version>
    </plugin> 

而我的配置 xml 包含

   <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="mypackage"/>
    <aop:aspectj-autoproxy proxy-target-class="true" />

我还在 Application 类中添加了以下注释

  @Configuration
  @EnableAspectJAutoProxy(proxyTargetClass=true)
  public class Configuration{
   ...
  }

在启动时,通过在 ApplicationContext 中记录 bean,我可以看到正在创建方面类“AuditLog”。

我设置了 2 个断点,但调试器不会在“beforeServcie”方法处停止,而是在“helloService”处停止。

我错过了什么?

【问题讨论】:

  • 试过了,没用
  • 你不需要所有这些 aspectj 依赖项
  • 为什么会有单独的xml配置?
  • proxyTargetClass=true 不需要
  • 哪些依赖是多余的?我已经删除了 targetClass=True,但它仍然没有从 Aspect 方法传递

标签: java maven spring-boot spring-aop aspect


【解决方案1】:

试试这个

execution(* mypackage.services.Service.*.*(..))

而不是

execution(* mypackage.services.Service.*(..))

【讨论】:

  • 如果你使用的是spring boot,不需要在config类中添加依赖,config.xml和@EnableAspectJAutoProxy(proxyTargetClass=true)
  • 如果我删除依赖项(即 spring-aop、aspectjrt),我会在“@Aspect”、“@AfterReturning”和“@Before”注释上得到编译错误
【解决方案2】:
  1. 如果您使用的是 spring-boot,那么您可以这样做,而不是自动添加依赖项 jars

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    
  2. 如果您使用 XML 配置 &lt;aop:aspectj-autoproxy ... /&gt;,则无需使用 @EnableAspectJAutoProxy。这可能无关紧要,因为 AFAIK XML 配置胜过注释配置,但最好避免重复

  3. 我不太确定你为什么需要aspectj-maven-plugin,因为 Spring 通过代理和 AFAIK 实现 AOP,这个插件仅在编译时、编译后或加载时编织是不同的概念时才需要,请参阅 Spring AOP vs AspectJ

现在上述所有要点可能无法解决您的问题,但以下可能

execution(* mypackage.services.Service.Service.*(..))

而且,不要设置proxyTargetClass=true,让它默认为false。

说明

格式为execution(&lt;return type&gt; &lt;package name&gt;.&lt;class name&gt;.&lt;method name&gt;(..)

这里的包名是mypackage.services.Service,类名是Service。

【讨论】:

  • 感谢三年后的回复。我从配置中删除了 proxyTargetClass=true 并使用了 @Before("execution(* mypackage.services.Service.*(..))") 但没有运气:(
  • @AhaduTsegayeAbebe 你说你试过mypackage.services.Service.* 但建议的是mypackage.services.Service.Service.*
  • 对不起,我忘了提到我已将包更正为“mypackage.services;”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
相关资源
最近更新 更多