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


一、加載自定義配置文件:

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

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM