Java Spring Boot@ConditionalOnProperty未动态设置属性
我有一个Spring启动应用程序和一个外部库。应用程序导入库。库中有一个@Configuration类,该类根据此属性的值有条件地加载。 我使用的是Spring Boot 2.2.6 相关类别如下所示:Java Spring Boot@ConditionalOnProperty未动态设置属性,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个Spring启动应用程序和一个外部库。应用程序导入库。库中有一个@Configuration类,该类根据此属性的值有条件地加载。 我使用的是Spring Boot 2.2.6 相关类别如下所示: @Configuration @ConditionalOnProperty(prefix = "custom", name = "enabled", havingValue = "true") public class LibraryT
@Configuration
@ConditionalOnProperty(prefix = "custom", name = "enabled", havingValue = "true")
public class LibraryTrueConfiguration {
// Load the Beans
}
应用程序的主类:
@Import(SharedLibConfiguration .class)
@SpringBootApplication
public class ServiceApp {
public static void main(final String[] args) {
SpringApplication.run(ServiceApp.class, args);
}
}
在应用程序的下面配置中动态设置属性,这些属性集不会由库反映以加载相关配置
如果我的application.yml有custom.enabled=false,并且我在下面的代码中设置custom.enabled=true,则不会在正在加载的配置文件库中反映出来。正在加载自定义的文件。enabled=false
@Configuration
public class DynamicPropsConfig {
// Service which fetch dynamic properties from DB
@Autowired
DynamicPropsLoader dynamicPropsLoader;
@Autowired
CustomProperties customProperties;
@DependsOn("dynamicPropsLoader")
@Autowired
void setDynamicPropertiesInEnvironment(ConfigurableEnvironment environment) {
System.out.println("[Before]customProperties = " + customProperties);
environment.getPropertySources().addFirst(new MapPropertySource("custom",
dynamicPropsLoader.getCustomPropertiesMap()));
final HashMap<Object, Object> customPropertiesMap = dynamicPropsLoader
.getCustomPropertiesMap().keySet()
.stream()
.collect(HashMap::new, (m, v) -> m.put(v, environment.getProperty(v)), HashMap::putAll);
System.out.println("[After]customPropertiesMap = " + customPropertiesMap);
}
}
这帮助我解决了这个问题。我们需要使用EnvironmentPostProcessor和前面提到的META-INF/spring.factories文件
@Import(SharedLibConfiguration .class)
@SpringBootApplication
public class ServiceApp {
public static void main(final String[] args) {
SpringApplication.run(ServiceApp.class, args);
}
}
@Configuration
public class DynamicPropsConfig {
// Service which fetch dynamic properties from DB
@Autowired
DynamicPropsLoader dynamicPropsLoader;
@Autowired
CustomProperties customProperties;
@DependsOn("dynamicPropsLoader")
@Autowired
void setDynamicPropertiesInEnvironment(ConfigurableEnvironment environment) {
System.out.println("[Before]customProperties = " + customProperties);
environment.getPropertySources().addFirst(new MapPropertySource("custom",
dynamicPropsLoader.getCustomPropertiesMap()));
final HashMap<Object, Object> customPropertiesMap = dynamicPropsLoader
.getCustomPropertiesMap().keySet()
.stream()
.collect(HashMap::new, (m, v) -> m.put(v, environment.getProperty(v)), HashMap::putAll);
System.out.println("[After]customPropertiesMap = " + customPropertiesMap);
}
}
@Component
public class DynamicCustomPropsClr implements CommandLineRunner {
@Autowired
Environment environment;
@Autowired
ConfigurableEnvironment configurableEnvironment;
@Value("${custom.enabled:}")
String customEnabled;
@Override
public void run(String... args) throws Exception {
System.out.println("environment.getProperty(\"custom.enabled\") = " + environment
.getProperty("custom.enabled"));
System.out.println(
"configurableEnvironment.getProperty(\"custom.enabled\") = " + configurableEnvironment
.getProperty("custom.enabled"));
System.out.println("customenabled = " + customenabled);
}
}
output
------
environment.getProperty("custom.enabled") = true
configurableEnvironment.getProperty("custom.enabled") = true
customEnabled = false