多模块项目中的Spring Boot AOP在建议之前不执行

问题描述 投票:0回答:2

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

parentProject | __aop | __data | __web

Web模块在de.my.awesome.project.web.service包中有一个用户服务类,其中包含一个保存用户方法:

@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
2个回答
0
投票

@Configuration - 表示此文件包含Aspect的Spring Bean配置。

将@Component替换为@Configuration for LoggingAspect。


0
投票

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

我不得不在web模块pom中为我的aop模块添加一个依赖项,反之亦然。然后我添加了一个导入我的AspectsConfig到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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.