条件@PostConstruct注释

jit*_*jit 6 spring conditional javabeans postconstruct

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

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

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

jcr*_*ada 1

据我所知,条件不能与后构造混合。所以我的建议

  1. 添加两种不同的 bean,一种带有 postconstruct,另一种不带 as@Conditional在 bean 声明中使用

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

    import org.springframework.core.env.Environment;
    
    private final Environment env;
    
    public void execute() {
        if (env.getProperty("condition") == "condition") {
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使用 1 中的近似值,但 @Profile传递活动轮廓作为参数-Dspring.profiles.active=my_profile