springboot2.0入门(七)-- 自定义配置文件+xml配置文件引入

时间:2024-03-07 19:36:59

一、加载自定义配置文件:

1、新建一个family.yam文件,将上application.yml对象复制进入family

family:
  family-name:
  dad:
    name: levi
    age: 30  #${random.int}  随机数的值是不能传递的
  mom:
    alias:
      - yilisha
      - alise
    age: ${family.dad.age}   #妈妈的年龄和爸爸相同,没有则默认为24岁
  child:
    name: happlyboy
    age: 5
    friends:
      - {hobby: baseball,sex: male}
      - {hobby: football,sex: famale}

 

2、自定义一个配置类:

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();
    }
}

 

该配置了除了可以引入springboot默认的application.properties文件,还能引入自定义的yml文件

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

在family类上加入上述注解,如果是读取properties配置文件,只需要加@PropertySource(value = {"classpath:family.properties"})即可。不需要定义MixPropertySourceFactory。

 

 application配置文件的优先级是比普通的yml配置文件优先级是要高的,相同属性的配置,只有字application中没有配置才能生效

二、老的文件配置引入(xml文件)

1、自定义一个xml配置文件

<?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、在springboot启动时配置装载xml文件

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

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

}

 

3、新建一个测试类:

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

        @Autowired
        private ConfigurableApplicationContext ioc;

        @Test
        public void testHelloService() {
            //测试Spring上下文环境中是否有testBeanService这样一个bean,有的话表示xml配置文件生效
            boolean testBeanService= ioc.containsBean("testBeanService");
            System.out.println(testBeanService);
        }
}

 

4、运行结果:

 

 spring中注入了目标bean