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