Spring Boot sub-environment export custom XML configuration

Source: Internet
Author: User

Background Introduction:

Because the new spring boot project needs to use the old jar package, the configuration of the old jar package is configured in XML, and the development development, test tests, integration off, and formal production environments will be different. At this point, we need to have the spring boot sub-environment load different XML configuration files.


Program Introduction:

Spring boot @ConditionalOnProperty annotations allow us to load @configuration configurations based on conditions; @ImportResource annotations to import XML configuration files application-environment. YML Mode you can configure different attribute values for different environments. With these 3 features, we can implement the purpose of loading the XML configuration file in a sub-environment.


Specific practice steps:

(1) Add configuration in application-development.yml:importenv:development

(2) Add configuration in application-test.yml:importenv:test

(3) The XML configuration that will be loaded in the environment is divided into multiple files, each of which writes the respective environment-related configuration, for example:spring-development.xml,spring-test.xml

(4) Join the configuration class

@Configurationpublic  class ResourceConfig {     @Configuration       @ConditionalOnProperty (name =  "importenv", havingvalue =  "development")      @ImportResource (locations={"Classpath:spring-development.xml"})      class ResourceDevelopment{         @Bean          public test test () {         System.out.println ("==================development=========================");         return new test ();        }     }     @Configuration      @ConditionalOnProperty (name =   "importenv", havingvalue =  "test")      @ImportResource (locations={" Classpath:spring-test.xml "})     class ResourceTest{         @Bean          public test test () {         system.out.println ("==================test=========================");         return new test ();        }     }}

Test:

Modify the environment variable to start in the APPLICATION.YML configuration, you can see different environments loaded with different configurations

#spring Active Envspring:application:name:test1 Profiles:active:development




Spring Boot sub-environment export custom XML configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.