对于spring boot +集成应用程序,我试图从数据库加载配置,允许Spring Environment
&可以通过@Value
注释注入,并且可以通过外部化配置覆盖,如Externalized Configuration Section下的spring引用参考文档中所述。
我遇到的问题是我的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<String,Object> 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
:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:int-file="http://www.springframework.org/schema/integration/file"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd" default-lazy-init="true">
<int-file:inbound-channel-adapter channel="input2" directory="${input-dir}" filename-pattern="*">
<int:poller fixed-rate="500"/>
</int-file:inbound-channel-adapter>
<int:service-activator input-channel="input2" ref="sampleEndpoint" method="hello" output-channel="output2"/>
<int:channel id="output2"/>
<int-file:outbound-channel-adapter channel="output2" directory="${output-dir}"/>
</beans:beans>
我得到的错误:
2015-10-28 17:22:18.283 INFO 3816 --- [ main] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [si-common-context.xml]
2015-10-28 17:22:18.383 INFO 3816 --- [ main] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [si-mail-in-context.xml]
2015-10-28 17:22:18.466 INFO 3816 --- [ main] o.s.b.f.config.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] o.s.i.config.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] o.s.b.f.s.DefaultListableBeanFactory : 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配置之后,弹簧加载DbPropertiesConfig
。
如何解决此问题?
提前致谢
AP
答案 0 :(得分:0)
是的,我可以通过家庭测试确认在注释内容之前加载了XML定义。确定原因是否足够复杂,但我确信@Import*
资源在前提下比内部@Configuration
逻辑更重要。
因此,@PostConstruct
不适合与XML混合使用。
一种解决方案是将所有Spring Integration配置移至Annotation样式,甚至考虑使用Spring Integration Java DSL。
另一种解决方案是遵循Spring Boot的外部化配置建议:
- 默认属性(使用
醇>SpringApplication.setDefaultProperties
指定)。
这意味着您必须在启动Spring应用程序之前从数据库中读取属性。是的,如果你打算在同一个应用程序中使用DataSource
,那将是不可能的。从另一侧开始,让我们再看看你的目标吧!您将从DB作为外部配置加载应用程序的属性,那么,应用程序本身有什么意义呢?当然,在应用程序启动之前加载和提供属性会更安全。
希望我很清楚。