简体   繁体   English

Spring Boot外部配置和xml上下文

[英]Spring Boot external configuration and xml context

I want to externalize my configuration with Spring Boot, but I want to continue to partially use my xml context. 我想通过Spring Boot外部化我的配置,但我想继续部分使用xml上下文。

My main class SpringServerApplication.java : 我的主类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);
    }

}

I put my configuration in application.properties. 我将配置放在application.properties中。

And in ApplicationContextServer.xml, I want to use some parameter like this : ${user}. 在ApplicationContextServer.xml中,我想使用一些这样的参数:$ {user}。

But it does not work. 但这行不通。 Thanks in advance for your help. 在此先感谢您的帮助。

Remove the @PropertySource as that is already done by Spring Boot, instead add @EnableAutoConfiugration and use @ImportResource to import your xml config files. 删除@PropertySource就像Spring Boot已经完成的那样,而是添加@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);
    }
}

That should be enough to do what you want. 那应该足以做您想要的。 Depending on the content in your xml file you might even be able to drop some of it (as Spring Boot can quite easily do auto configuration of resources for you). 根据xml文件中的内容,您甚至可以删除其中的一些内容(因为Spring Boot可以很容易地为您自动配置资源)。

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

And import xml based configuration like this: 并导入基于xml的配置,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM