spring-boot读取自定义文件二:yml文件

       写在前面:继上一篇读取自定义properties文件这一片主要介绍读取自定义yml文件。

      上一篇可参见https://blog.csdn.net/qq_22596931/article/details/86604426

      涉及文件:define.yml(自定义yml文件)、DefineYml.java(绑定的java bean)、YamlPropertySourceFactory.java(自定义)、DefineController.java(用于测试的)。

1:define.yml文件

---
#shop: 
#  name: xiaomi6
baseinfo:
  name: wch
  sex: 男
  age: 23
#moreinfo: 
#  addr: 南昌
#  hobby: 待定
... 
#关于yml文件的编写规范
#1:在每一个属性后面打了冒号后要再按一次空格键,再输入对应的属性值,入上面标绿的是规范的能够被读取到,
#而最下面属性和属性值之间没有空格,不是规范的,文件读取不到,在启动程序就报错,这个地方需要注意。
#2:关于文档的起止符(---和...),起始符号和终止符号都要从行首开始,如果是起始符号或者终止符号前面有空格的话,就无法正确读取,启动报错。
#wch:
 # baseinfo:
  #  name:wch
   # sex:男
   # age:22
 # moreInfo:
  #  addr:南昌
   # hobby:待定

2:YamlPropertySourceFactory.java文件

package com.wch.SpringBootTestDemo.defineTest;

import java.io.IOException;

import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.boot.env.PropertySourcesLoader;


public class YamlPropertySourceFactory implements PropertySourceFactory {

	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		
		@SuppressWarnings("rawtypes")
		PropertySource test = name != null ? new PropertySourcesLoader().load(resource.getResource(), name, null) : new PropertySourcesLoader().load(  
                resource.getResource(), getNameForResource(resource.getResource()), null); 
		
		return test;
	}
	
	private static String getNameForResource(Resource resource) {  
        String name = resource.getDescription();  
        if (!org.springframework.util.StringUtils.hasText(name)) {
            name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);  
        }  
        return name;  
    }


}

3:DefineYml.java文件

package com.wch.SpringBootTestDemo.defineTest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 测试yml文件读取
 * 在这里配置prefix,但是貌似时没用,还需要进行测试
 * @author 19821
 *
 */
@Component
@ConfigurationProperties(prefix="baseinfo")
//@PropertySource("classpath:define.yml")
@PropertySource(value = {"classpath:define.yml"}, factory = YamlPropertySourceFactory.class)
public class DefineYml {

	/*
	@Value("${shop.name}")
    private String name;*/
	
	@Value("${baseinfo.name}")
	private String baseName;
	
	@Value("${baseinfo.sex}")
	private String baseSex;
	
	@Value("${baseinfo.age}")
	private String baseAge;
/*	
	@Value("${moreinfo.addr}")
	private String addr;
	
	@Value("${moreinfo.hobby}")
	private String hobby;*/

	/*public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}*/

	public String getBaseName() {
		return baseName;
	}

	public void setBaseName(String baseName) {
		this.baseName = baseName;
	}

	public String getBaseSex() {
		return baseSex;
	}

	public void setBaseSex(String baseSex) {
		this.baseSex = baseSex;
	}

	public String getBaseAge() {
		return baseAge;
	}

	public void setBaseAge(String baseAge) {
		this.baseAge = baseAge;
	}

/*	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	*/
	
	
	
}

4:DefineController.java

apackage com.wch.SpringBootTestDemo.defineTest;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/define")
public class DefineController {

	@Autowired
	private DefineEntity defineEntity;
	
	@Autowired
	private DefineYml defineYml;
	
	/**
	 * 这里测试自定义文件
	 * 测试读取自定义properties形式文件内容
	 * @return
	 */
	@RequestMapping("/getDefine")
	@ResponseBody
	public String getDefineInfo(){
		String str = "test.name="+defineEntity.getPname()
		+"----test.password="+defineEntity.getPassword()
		+"---test.testUp="+defineEntity.getTestUpcase()
		+"testOnly="+defineEntity.getTestOnly();
		return str;
	}
	
	/**
	 * 这里测试读取测试自定义文件
	 * 测试读取自定义的yml形式的文件内容
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getYmlDefine")
	public String getYmlDefineInfo(){
/*		String str = "wch.baseinfo.name="+defineYml.getName()+"---wch.baseinfo.sex="+defineYml.getSex()
		+"----wch.baseinfo.age="+defineYml.getAge()+"---wch.moreInfo.addr="+defineYml.getAddr()
		+"---wch.moreInfo.hobby="+defineYml.getHobby();*/
	//	String str = "name="+defineYml.getName();
		
	/*	String str = "shop.name="+defineYml.getName()+"---baseName="+defineYml.getBaseName()
					 +"---baseSex="+defineYml.getBaseSex()+"---baseAge="+defineYml.getBaseAge()
					 +"---moreAddr="+defineYml.getAddr()+"---moreHobby="+defineYml.getHobby();
		*/
		String str = "---baseName="+defineYml.getBaseName()
		 +"---baseSex="+defineYml.getBaseSex()+"---baseAge="+defineYml.getBaseAge();
		
	//	String str = "---baseName="+defineYml.getBaseName();
		return str;
	}

}

测试效果:

猜你喜欢

转载自blog.csdn.net/qq_22596931/article/details/86606903