自定义 Spring @Profile 注解

Pav*_*cak 6 spring annotations spring-boot

我正在将我的@SpringBootApplication应用程序从 Spring Boot 1.5 更新到 2.2,将 Spring 4.3 更新到 5.2,并且我遇到了自定义问题@Profile注释的问题。

在旧版本中,我有注释

@Profile("sapConnector")
   public @interface SapConnectorProfile {
}
Run Code Online (Sandbox Code Playgroud)

并且属于“sapConnector”的每个 bean 都用该@SapConnectorProfile注释进行了注释。当我运行应用程序时,我只需定义属性spring.profiles.active=sapConnector并加载用此注释注释的 bean。如果我将属性更改为 fe,spring.profiles.active=demoConnector它不会加载任何 bean@SapConnectorProfile annotation.

在新版本的 Spring 中,@SapConnectorProfile即使属性spring.profiles.active=demoConnector.

在新的Spring中不能再这样做了吗?

@Profile("sapConnector")如果我使用注释而不是,配置文件工作正常@SapConnectorProfile.

谢谢你的帮助。

Pav*_*cak 4

我缺少@Retention(RetentionPolicy.RUNTIME)注释。

当我使用时一切正常:

@Retention(RetentionPolicy.RUNTIME)
@Profile("sapConnector")
public @interface SapConnectorProfile {
}
Run Code Online (Sandbox Code Playgroud)

我创建的问题:https ://github.com/spring-projects/spring-framework/issues/23901