Spring Boot外部配置和xml上下文



我想用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}。

但它不起作用。提前感谢您的帮助。

按照Spring Boot已经完成的操作删除@PropertySource,而是添加@EnableAutoConfiugration并使用@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可以很容易地为您自动配置资源)。

applicationContext.xml 中使用<context:property-placeholder/>

导入基于xml的配置,如下所示:

@ImportResource({"classpath*:applicationContext.xml"})

最新更新