简体   繁体   中英

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.

My main class 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.

And in ApplicationContextServer.xml, I want to use some parameter like this : ${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.

@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).

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

And import xml based configuration like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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