Spring @AliasFor 不适用于@Profile 注释

Spring @AliasFor not working for @Profile annotation

Environment: Kotlin 1.5.30, SpringBoot 2.5.4(Spring 5.3.9)

背景与问题

我正在尝试创建一个组合注释来简化类似的模板注释代码。注解class是这样的:

@Profile("default")  //NOTE 1: use a placeholder, see the investigations below
annotation class ProfileAware(
    @get: AliasFor(annotation = Profile::class, attribute = "value")
    val profiles: Array<String>,
    //properties delegated for other annotations
)

预期用途:

@Component
@ProfileAware(profiles = ["dev"])
class TheBean {

    init {
        LoggerFactory.getLogger("TheBean").info("TheBean: I'm registered")
    }
}

在application.yaml中:

spring:
  profiles:
    active: dev

但是应用启动后,TheBean没有按预期注册。

调查与尝试

我做错了什么吗?或者是否可以创建具有动态 @Profile 值的组合注释?

感谢您的阅读和帮助。

@Profileorg.springframework.context.annotation.ProfileCondition 查找,其 matches(...) 方法使用 org.springframework.core.type.AnnotatedTypeMetadata.getAllAnnotationAttributes(String) 查找 @Profile 注释,该方法的 Javadoc 指出以下(我添加的粗体强调)。

Retrieve all attributes of all annotations of the given type, if any (i.e. if defined on the underlying element, as direct annotation or meta-annotation). Note that this variant does not take attribute overrides into account.

因此,您目前无法将 @Profile@AliasFor 一起用于注释属性覆盖。