我想使用 Spring Boot 外部化我的配置,但我想继续部分使用我的 xml 上下文。
我的主类 SpringServerApplication.java :
@Configuration
@PropertySources(value = {@PropertySource("classpath:/application.properties")})
public class SpringServerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(new Object[] {
SpringServerApplication.class, "classpath:ApplicationContextServer.xml" }, args);
}
}
我将配置放在 application.properties 中。
在ApplicationContextServer.xml中,我想使用一些像这样的参数:${user}。
但是它不起作用。预先感谢您的帮助。
最佳答案
删除 @PropertySource
,因为 Spring Boot 已完成此操作,而是添加 @EnableAutoConfiguration
并使用 @ImportResource
导入您的 xml 配置文件。
@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:ApplicationContextServer.xml")
public class SpringServerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(new Object[] {SpringServerApplication.class}, args);
}
}
这应该足够做你想做的事了。根据 xml 文件中的内容,您甚至可以删除其中的一些内容(因为 Spring Boot 可以很轻松地为您自动配置资源)。
关于java - Spring Boot 外部配置和 xml 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634426/