【问题标题】:Spring Boot AOP in multi module project does not execute Before Advice多模块项目中的 Spring Boot AOP 不执行之前的建议
【发布时间】:2019-08-29 15:06:36
【问题描述】:

我正在使用 Springs AOP 迈出第一步,并想从一个简单的日志记录建议开始。我的项目是一个多模块的maven项目,结构如下:

父项目
|__aop
|__数据
|__网络

web 模块在 de.my.awsome.project.web.service 包中有一个用户服务类,带有 saveNewUser 方法:

@Service
public class UserService {
...
    public MdUser saveNewUser(UserModel user) {
        MdUser newUser = this.save(user);
        createGroupMembership(user, newUser);
        return newUser;
    }
}

该方法按预期工作,因此不必为此烦恼。

我现在所做的是在 aop 模块中创建以下类:

@Component
@Aspect
public class LoggingAspect {

    Logger logger = Logger.getLogger(getClass());

    @Before("execution(public * de.my.awsome.project.web.service.UserService.saveNewUser(..))")
    public void newUserLog(JoinPoint joinpoint) {
        logger.info(joinpoint.getSignature() + " with user " + joinpoint.getArgs()[0]);
}

}

我在aop模块的pom中添加了web模块的依赖:

<dependency>
    <groupId>de.my.awsome.project</groupId>
    <artifactId>web</artifactId>
    <version>${project.version}</version>
</dependency>

我什至写了一个 ConfigurationClasse,尽管我认为 SpringBoot 不需要这样做:

@Configuration
@ComponentScan(basePackages="de.fraport.bvd.mobisl.aop")
public class AspectsConfig {

}

预期的结果是类似“saveNewUser with user xyz”的日志消息。但是永远不会调用日志记录方法。我错过了什么?

【问题讨论】:

    标签: maven spring-boot spring-aop multi-module


    【解决方案1】:

    @Configuration - 表示此文件包含一个方面的 Spring Bean 配置。

    用 @Configuration 替换 LoggingAspect 的 @Component。

    【讨论】:

      【解决方案2】:

      嗯,@sankar 发布的答案也不起作用,但我自己找到了解决方案。

      我必须在 web 模块 pom 中为我的 aop 模块添加一个依赖项,反之亦然。然后我将我的 AspectsConfig 的 Import 添加到 Web 模块 SpringBootApplication 类中并且它起作用了。

      @SpringBootApplication
      @Import(value= {JPAConfig.class, AspectsConfig.class})
      @EnableAspectJAutoProxy
      public class WebApplication {
      
      @Autowired
      private JPAConfig config;
      
      @Autowired
      private AspectsConfig aspectConfig;
      
          public static void main(String[] args) {
              SpringApplication.run(WebApplication.class, args);
          }
      }
      

      【讨论】:

        【解决方案3】:

        以下步骤对我有用 - 如果有人正在寻找解决方案,请参阅下文

        1. 在主父 pom.xml 中添加 aop 依赖
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
        
        1. 在他的 aop-sub-module 中添加你的 Aspect 实现
           @Aspect
           @Component
           public class SampleAspect {
        
            @After("execution(* de.my.awsome.project.testMethod())") 
            public void logAuditActivity(JoinPoint jp) { 
             System.out.println("TESTING ****************");
             System.out.println("The method is called");
        
           }
        
        1. 在你的其他子模块(web-submodule)中添加aspect-submodule的依赖
            <dependency>
                <groupId>de.my.awsome.project</groupId>
                <artifactId>aop</artifactId>
            </dependency>
        
        1. 在您的 Web 项目中包含用于组件扫描的 aop 包 例如。如果 SampleAspect 在 de.my.awsome.project 包下,您需要按如下方式添加
            @ComponentScan(basePackages = {"de.my.awsome.project", "de.my.awsome.aop" }
        
        1. 干净构建并运行应用程序

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-07
          • 2019-05-24
          • 1970-01-01
          • 2015-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多