java匹配yml文件内容,SpringBoot获取yml和properties配置文件的内容

(一)yml配置文件:

pom.xml加入依赖:

org.springframework.boot

spring-boot-configuration-processor

${spring-boot.version}

在application.yml文件中加上:

#自定义的属性和值

myYml:

simpleProp: simplePropValue

arrayProps: 1,2,3,4,5

listProp1:

- name: abc

value: abcValue

- name: efg

value: efgValue

listProp2:

- config2Value1

- config2Vavlue2

mapProps:

key1: value1

key2: value2

使用一个java类获取yml文件的内容:

package com.sun.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* 加载yaml配置文件的方法

* Created by sun on 2017-1-15.

* spring-boot更新到1.5.2版本后locations属性无法使用

* @PropertySource注解只可以加载proprties文件,无法加载yaml文件

* 故现在把数据放到application.yml文件中,spring-boot启动时会加载

*/

@Component

//@ConfigurationProperties(locations = {"classpath:config/myProps.yml"},prefix = "myProps")

@ConfigurationProperties(prefix = "myYml")

public class YmlConfig {

String simpleProp;

private String[] arrayProps;

private List> listProp1 = new ArrayList<>(); //接收prop1里面的属性值

private List listProp2 = new ArrayList<>(); //接收prop2里面的属性值

private Map mapProps = new HashMap<>(); //接收prop1里面的属性值

public String getSimpleProp() {

return simpleProp;

}

//String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要

public void setSimpleProp(String simpleProp) {

this.simpleProp = simpleProp;

}

public String[] getArrayProps() {

return arrayProps;

}

public void setArrayProps(String[] arrayProps) {

this.arrayProps = arrayProps;

}

public List> getListProp1() {

return listProp1;

}

public void setListProp1(List> listProp1) {

this.listProp1 = listProp1;

}

public List getListProp2() {

return listProp2;

}

public void setListProp2(List listProp2) {

this.listProp2 = listProp2;

}

public Map getMapProps() {

return mapProps;

}

public void setMapProps(Map mapProps) {

this.mapProps = mapProps;

}

}

通过依赖注入就可以获取该对象:

@Autowired

private YmlConfig config;

方法内获取值:

ObjectMapper objectMapper = new ObjectMapper();

//测试加载yml文件

System.out.println("simpleProp: " + config.getSimpleProp());

System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));

System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));

System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));

System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));

(二)properties配置文件:

使用@PropertySource注解加载配置文件,该注解无法加载yml配置文件。使用@Value注解获得文件中的参数值

package com.sun.configuration;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**

* 加载properties配置文件,在方法中可以获取

* abc.properties文件不存在,验证ignoreResourceNotFound属性

* 加上encoding = "utf-8"属性防止中文乱码,不能为大写的"UTF-8"

* Created by sun on 2017-3-30.

*/

@Configuration

@PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/abc.properties"},

ignoreResourceNotFound = true,encoding = "utf-8")

public class PropConfig {

// PropertySourcesPlaceholderConfigurer这个bean,

// 这个bean主要用于解决@value中使用的${…}占位符。

// 假如你不使用${…}占位符的话,可以不使用这个bean。

@Bean

public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

return new PropertySourcesPlaceholderConfigurer();

}

}

//获取properties文件参数值有两种方法,一种获得Environment 的对象,第二种就是@Value注解

@Autowired

private Environment env;

@Value("${age}")

String name;

@RequestMapping("/")

@ResponseBody

String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {

logger.info("测试通过!!!");

ObjectMapper objectMapper = new ObjectMapper();

//测试加载yml文件

System.out.println("simpleProp: " + config.getSimpleProp());

System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));

System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));

System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));

System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));

//测试加载properties文件

System.out.println(env.getProperty("name"));//孙凯

System.out.println(env.getProperty("abc"));//null

System.out.println(name);//26

return "Hello World!";

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值