是否可以使用应用程序属性来设置Spring boot中bean的定义?
例如:
@Bean
public SomeClassImpl getImpl(){
return SomeClassImplFactory.getDefaultClassImpl();
}
将是默认定义。但是有没有一种方法可以在属性文件中指定类似
getImpl=some.package.NewClassImpl
覆盖前一个?
谢谢
最佳答案
您可以考虑使用这种方法:
@Autowired
private Environment environment;
@Bean
public SomeClassImpl getImpl(){
if(environment.getProperty("getImpl").equals("xxx")) {
return new NewClassImpl();
}
return SomeClassImplFactory.getDefaultClassImpl();
}
关于java - 在 Spring Boot 中使用应用程序属性动态更改 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60353899/