我正在使用spring cloud(带有spring boot)来创建一些微服务。现在我已经为其中一项服务启用了石英调度。以下是代码段(修剪):
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setOverwriteExistingJobs(true);
factory.setDataSource(dataSource);
factory.setJobFactory(jobFactory);
factory.setAutoStartup(true);
factory.setQuartzProperties(quartzProperties());
return factory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
quartz.properties:
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = default
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
现在我不喜欢这个quartz.properties文件,并希望通过spring cloud配置服务器共享此配置(我已经有了)。如果我将这些配置放在那里,我可以通过@Value
和@ConfigurationProperties
来完成这些配置,但我无法获取如何将java.util.Properties
作为SchedulerFactoryBean.setQuartzProperties()
传递给{{1} }}。我不想逐一阅读并设置。
答案 0 :(得分:2)
您无法将Properties
绑定到@ConfigurationProperties
,但您可以绑定Map<String,String>
并手动转换。
答案 1 :(得分:0)
以下是让它发挥作用的一种方法:
@Bean
public Properties quartzProperties() {
Properties properties = new Properties();
properties.putAll(StreamSupport
.stream(((AbstractEnvironment) env).getPropertySources().spliterator(), false)
.filter(propertySource -> propertySource instanceof EnumerablePropertySource)
.map(propertySource -> (EnumerablePropertySource) propertySource)
.map(EnumerablePropertySource::getPropertyNames)
.flatMap(Arrays::stream)
.filter(propertyName -> propertyName.startsWith("quartz."))
.collect(Collectors.toMap(
propertyName -> propertyName.replaceAll("^quartz\\.", ""),
propertyName -> env.getProperty(propertyName),
(key1, key2) -> key1
)));
return properties;
}