springboot 自定义配置文件,读取配置文件

springboot  大大的简便了开发动作,这里记录一种自定义配置,并读取配置信息的方法,不多废话,直接上代码

@ConfigurationProperties(prefix="my-props")

这个注解的意思是获取application 配置文件下的所有的以  my-props  开头的配置信息;

以此下面代码为例子


/**
 * 自定义配置文件
 * @author cdj
 * @date 2018年8月17日 下午3:12:20
 */
@Component  
@ConfigurationProperties(prefix="my-props")
public class MyPropsUtils {
    private Map<String, String> mapProps = new HashMap<>(); 
    private String root;
    private String web;

	public Map<String, String> getMapProps() {
		return mapProps;
	}

	public void setMapProps(Map<String, String> mapProps) {
		this.mapProps = mapProps;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public String getWeb() {
		return web;
	}

	public void setWeb(String web) {
		this.web = web;
	}

}

application.yml的内容是

  myProps: 
    mapProps:  
     width1: 45  
     width2: 200 
     width3: 30
    root: D:/test/systemfile
    web: web

这样再掉用MyPropsUtils 时,会自动获取到配置文件里面的信息,这是springboot 读取的一种机制。也是很方便的。

猜你喜欢

转载自blog.csdn.net/atmknight/article/details/81777706