【问题标题】:Conditional @PostConstruct annotation有条件的@PostConstruct 注解
【发布时间】:2018-03-15 14:04:12
【问题描述】:

我有时可能不需要根据通过 JVM 参数传递的值在启动时对 bean 进行后期构造。

我尝试了@Conditional 注释,但它只适用于@Bean 注释。

您以前尝试过/需要这样的东西吗?

【问题讨论】:

    标签: spring conditional javabeans postconstruct


    【解决方案1】:

    AFAIK Conditional 不能与 postconstruct 混合使用。所以我的建议

    1. 在 beans 声明中添加两个不同的 beans,一个带有 postconstruct,一个没有 as use @Conditional

    2. 添加验证您的应用程序参数的程序代码,例如:

      import org.springframework.core.env.Environment;
      
      private final Environment env;
      
      public void execute() {
          if (env.getProperty("condition") == "condition") {
          }
      }
      
    3. 在 1 中使用近似值但 @Profile 并将活动配置文件作为参数 -Dspring.profiles.active=my_profile 传递

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-21
      • 2017-11-24
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多