V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ftsland
V2EX  ›  Java

Spring Aop 跨模块失效求助

  •  
  •   ftsland · 4 天前 · 298 次点击

    https://stackoverflow.com/questions/64814633/spring-aop-aspect-doesnt-work-in-multi-module-project

    遇到一个需求需要把分布在各个模块的接口中返回的某些字段加密, 把切面 @Aspect 定义在了公共模块 common 业务模块 A,B 在 pom 依赖 common 模块, 参照 stackoverflow 的答案在 A,B 模块分别定义了 Config 类 切点使用自定义注解, 在公共模块中的接口(没错。。老代码里公共模块也有对外接口)切面生效了, 但是在 A,B 模块的接口切面一直不生效, 求助

    //切面类
    
    @AfterReturning(pointcut = "@annotation(cn.core.annotation.InfoEncrypt)", returning = "response")
    public void afterReturning(JoinPoint jp, InfoEncryptResponse response) {
        LoggerEx.info("加密身份证,手机号开始");
        try {
            if (response != null && CollectionUtils.isNotEmpty(response.getInfoEncryptBeans())) {
                List<? extends InfoEncryptBean> needEncryptBeans = response.getInfoEncryptBeans();
                doEncrypt(needEncryptBeans);
            }
        } catch (Exception e) {
            LoggerEx.exception("AspectInfoEncrypt exception", e);
        }
    }
    
    
    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @ComponentScan({"cn.module.a"})
    public class AspectInfoEncryptConfigA {
    
        @Bean
        public AspectInfoEncrypt aspectInfoEncrypt() {
            return new AspectInfoEncrypt();
        }
    
    }
    
    4 条回复
    mufanzz
        1
    mufanzz  
       4 天前   ❤️ 1
    失效的类被加入 spirng bean 了吗,调用的时候通过代理了吗?
    ftsland
        2
    ftsland  
    OP
       4 天前
    @mufanzz 切面不生效的类是使用 @Service 注解的 , 已经注入了
    yuezk
        3
    yuezk  
       4 天前   ❤️ 1
    确认一下 A, B 模块里面的返回值类型是不是 `InfoEncryptResponse` 或者它的子类
    ftsland
        4
    ftsland  
    OP
       4 天前
    想起来了, 是因为同类调用的问题, 学艺不精,, 惭愧,已经解决了, 谢谢两位的帮助

    @mufanzz
    @yuezk
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2151 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 00:56 · PVG 08:56 · LAX 17:56 · JFK 20:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.