springboot2.0 entry (seven) - Custom Profile + xml configuration file introduction

First, load the custom configuration file:

1, a new family.yam file, the copied object into the family on application.yml

Family: 
  Family - name: 
  DAD: 
    name: Levi 
    Age: 30 {# $. Random int } is the value of the random number can not be transmitted 
  MOM: 
    Alias:
       - yilisha
       - ALISE 
    Age: $ {#} mother family.dad.age the same age and my father, not the default is 24-year-old 
  Child: 
    name: happlyboy 
    Age: 5 
    Friends:
       - {Hobby: Baseball, Sex: MALE}
       - {Hobby: Football, Sex: famale}

 

2, a custom configuration class:

package com.liyu.helloworld.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.util.Properties;

public class MixPropertySourceFactory extends DefaultPropertySourceFactory {


    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        String sourceName = name != null ? name : resource.getResource().getFilename();
        if (!resource.getResource().exists()) {
            return new PropertiesPropertySource(sourceName, new Properties());
        } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
            Properties propertiesFromYaml = loadYml(resource);
            return new PropertiesPropertySource(sourceName, propertiesFromYaml);
        } else {
            return super.createPropertySource(name, resource);
        }
    }

    private Properties loadYml(EncodedResource resource) throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

 

This configuration can be introduced in addition to the default springboot application.properties file, but also the introduction of a custom file yml

@PropertySource(value = {"classpath:family.yml"}, factory = MixPropertySourceFactory.class)
public class Family {

Was added to the annotation in the family category, if it is to read the configuration file properties, just add @PropertySource(value = {"classpath:family.properties"})to. No definition MixPropertySourceFactory.

 

 Priority application configuration file is yml profile than the average is much higher priority, configured with the same properties, only the word application is not configured to take effect

Second, the introduction of the old configuration file (xml file)

1, a custom xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="testBeanService" class="com.liyu.helloworld.service.TestBeanService"></bean>
</beans>

 

2, when the loaded startup configuration xml file springboot

@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class Boot01HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(Boot01HelloworldApplication.class, args);
    }

}

 

3, create a test class:

@RunWith (SpringRunner.class) 
@SpringBootTest 
public class TestBean { 

        @Autowired 
        Private ConfigurableApplicationContext IOC; 

        @Test 
        public void testHelloService () { 
            // test Spring context whether there testBeanService such a bean, any expressed xml configuration file to take effect 
            boolean = ioc.containsBean testBeanService ( "testBeanService"); 
            System.out.println (testBeanService); 
        } 
}

 

4, the results:

 

 spring injected into the target bean

 

 

 

 

Guess you like

Origin www.cnblogs.com/liweiweicode/p/11824521.html