首页 > 解决方案 > 无法在方法上使用 Aspect

问题描述

这是在一个 Spring 项目中,我在其他模块所依赖的不同模块中设置了 Aspect。我正在使用自定义注释。它适用于某些方法,但不适用于其他方法。我可以就问题所在获得建议吗?

这个 Aspect 类来自一个模块,其他模块依赖于此。

@Aspect
@Component
public class VirtualizeJsonAspect {
    @Around("@annotation(virtualizeJson)")
    public Object virtualizeJsonAround(ProceedingJoinPoint pjp, VirtualizeJson virtualizeJson) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        Class returnType = signature.getReturnType();
    // ....... This is working fine
}

如果相关,在同一模块中关注 3 个类

当我尝试使用 Aspect 时,它在这里工作

参考路径 -> com/domain/abc/service/helper/DataHelper.java

@Component
@PropertySource("classpath:something.properties")
public class DataHelper {

    @VirtualizeJson(serviceNm = "ABC", virtualizedDataPath = "/url/some.json")
    public SomeResponse getOffers(SomeRequest someRequest){

        HttpEntity httpRequestEntity = createRequestEntity(request);

        ResponseEntity<SomeResponse> httpResponseEntity;
        SomeResponse someResponse = null;
        // ......... Aspect works here. I do not get in here. Instead I land in the Aspect class as expected
    }
}

配置文件路径供参考 -> com/domain/abc/service/config/AppConfig.java

@Configuration
@ComponentScan(basePackages = {{"com.domain.virtualize"})
@EnableAspectJAutoProxy
public class AppConfig extends WebMvcConfigurationSupport{
    // some bean setups not rlevant to Aspect
}

当我尝试使用 Aspect 时,它在这里不起作用

参考路径 -> com/domain/abc/service/rules/CheckSomething.java

@Component
public class CheckSomething extends SomeRule {

    @VirtualizeJson(serviceNm = "ABC", virtualizedDataPath = "/url/some.json")
    public SomeResponse checkOffers(String someNumber){

        int a = 1;
        return null;
        // I land inside this method which is incorrect. I shouldh have landed at the Aspect class instead 
    }
}

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface VirtualizeJson {
    public String serviceNm();
    public String virtualizedDataPath();
}

标签: javaspringaopaspectj

解决方案


推荐阅读