使用Spring Boot和Spring集成与数据库支持的配置

对于spring boot +集成应用程序,我正在尝试从数据库加载配置,允许它通过@Value注释可以访问Spring的Environment和注入,并且可以通过外部化配置覆盖,如弹簧引导参考文档中所述。 外化配置部分 。

我遇到的问题是我的spring Integration XML包含无法解析的${input}属性占位符,因为 Spring尝试加载XML配置之前 ,我无法加载数据库支持的配置。

应用程序的入口点:

 @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } } 

如何加载数据库配置:

  @Configuration public class DbPropertiesConfig { @Autowired private org.springframework.core.env.Environment env; @PostConstruct public void initializeDatabasePropertySourceUsage() { MutablePropertySources propertySources = ((ConfigurableEnvironment) env).getPropertySources(); try { // The below code will be replace w/ code to load config from DB Map map = new HashMap(); map.put("input-dir","target/input"); map.put("output-dir","target/output"); DbPropertySource dbPropertySource = new DbPropertySource("si",map); propertySources.addLast(dbPropertySource); } catch (Exception e) { throw new RuntimeException(e); } } } 

如何加载Spring Integration配置:

  @Profile("IN") @Configuration @ImportResource({"si-common-context.xml","si-input-context.xml"}) public class SiInputAppConfig { } 

Spring Integration XML配置si-input-context.xml

          

我得到的错误:

 2015-10-28 17:22:18.283 INFO 3816 --- [ main] osbfxml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [si-common-context.xml] 2015-10-28 17:22:18.383 INFO 3816 --- [ main] osbfxml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [si-mail-in-context.xml] 2015-10-28 17:22:18.466 INFO 3816 --- [ main] osbfconfig.PropertiesFactoryBean : Loading properties file from URL [jar:file:/C:/Users/xxx/.m2/repository/org/springframework/integration/spring-integration-core/4.1.6.RELEASE/spring-integration-core-4.1.6.RELEASE.jar!/META-INF/spring.integration.default.properties] 2015-10-28 17:22:18.471 INFO 3816 --- [ main] osiconfig.IntegrationRegistrar : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created. 2015-10-28 17:22:18.604 INFO 3816 --- [ main] osbfsDefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; ... 2015-10-28 17:22:18.930 WARN 3816 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0.source' defined in null: Could not resolve placeholder 'si.in-input' in string value "${si.in-input}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'input-dir' in string value "${input-dir}" ... 

尝试加载XML配置后, Spring加载DbPropertiesConfig

我该如何解决这个问题?

提前致谢

美联社

是的,我可以通过我的家庭测试确认在注释内容之前加载了XML定义。 确定原因是否足够复杂,但我确信@Import*资源比内部@Configuration逻辑更重要。

因此,@ @PostConstruct不适合与XML混合使用。

一种解决方案是将所有Spring Integration配置移动到Annotation样式,甚至考虑使用Spring Integration Java DSL 。

另一个解决方案是遵循Spring Boot的外部化配置建议:

  1. 默认属性(使用SpringApplication.setDefaultProperties指定)。

这意味着您必须在启动Spring应用程序之前从数据库中读取属性。 是的,如果您打算在同一个应用程序中使用DataSource ,那将是不可能的。 从另一方面来看,让我们再一次看看你的目标吧! 您将从DB作为外部配置加载应用程序的属性,那么,应用程序本身有什么意义呢? 当然,在应用程序启动之前加载和提供属性会更安全。

希望我很清楚。