如何创建像@PathVariable这样的自定义注释

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

我想制作一个像 @PathVariable 这样的自定义注释,将用于参数来为方法提供值,如下所示:

public String subscribe(@PathVariable String date, @PathVariable String text, @PermittedMarkets String[] marketCodes) { //do processing and return something here }

我已经定义了注释:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface PermittedMarkets {

}

和:

@Aspect
@Component
public class MarketsAspect {
    @Value("#{${config.market.permitted.marketmapping:{:}}}") //if value is provided, use it or default to empty map
    private Map<String,String> roleToMarketCodeMap;

    @Before("@annotation(com.org.app.service.security.annotation.PermittedMarkets)")
    public String[] before(JoinPoint joinPoint) {
        final var grantedAuthorities =
                Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
                        .map(Authentication::getAuthorities)
                        .orElseThrow(() -> new AccessDeniedException("No authorities found"));

        return grantedAuthorities.stream()
                .map(GrantedAuthority::getAuthority)
                .filter(entitlement -> entitlement.contains("app_user"))
                .map(A_entitlement -> roleToMarketCodeMap.get(A_entitlement))
                .toArray(String[]::new);
    }
}

但是,当我使用调试器单步调试时,订阅方法上显示的是以下内容:

调试器根本不会进入 before() 方法,即使上面有断点。

java spring spring-aop
1个回答
0
投票

请通读文档:Spring AOP 功能和目标

Spring AOP 目前仅支持方法执行连接点 (建议在 Spring beans 上执行方法)。

Spring AOP 不支持基于带注释的参数的建议。

© www.soinside.com 2019 - 2024. All rights reserved.