属性 有条件地启动 bean 组

Conditional initiation of group of beans by property

在我的 spring 引导应用程序中,我有许多负责监视的 bean。 今天,他们每个人都被注释为:

@ConditionalOnProperty(name="enable.monitor", havingValue="true")

这样监控是完全可配置的,我可以从属性文件中打开和关闭它。
在这个解决方案中,我不喜欢的是我必须向每个 bean 添加这个长注释。我想知道是否有更优雅的方式来实现这种可配置的监控。也许通过用一些新注释对所有 bean 进行注释,比如 @Monitoring,并以某种方式告诉 spring 根据 属性 启动它们,这可能吗?
欢迎任何其他建议。

可以创建您自己的自定义 @Monitoring 注释,如下所示:

Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ConditionalOnProperty(name="enable.monitor", havingValue="true")
public @interface Monitoring {
}

这之后可以应用于其他 bean。