有时我可能不需要根据通过 JVM 参数传递的值在启动时后构造 bean。
我尝试了 @Conditional
注释,但它仅适用于 @Bean
注释。
您以前尝试过/需要这样的东西吗?
最佳答案
据我所知,条件不能与后构造混合。所以我的建议
添加两个不同的 bean,一个带有 postconstruct,另一个没有在 bean 声明中使用
@Conditional
添加程序代码来验证您的应用程序参数,例如:
import org.springframework.core.env.Environment; private final Environment env; public void execute() { if (env.getProperty("condition") == "condition") { } }
使用 1 中的近似值,但
@Profile
并将事件配置文件作为参数传递-Dspring.profiles.active=my_profile
关于spring - 条件@PostConstruct注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560440/