springboot之yml配置文件用法

  1. yml 中不能用驼峰式写法(systemParams)要改成system-params
  2. 加载自定义yml文件
Spring Framework有两个类加载YAML文件,YamlPropertiesFactoryBean和YamlMapFactoryBean
通过PropertySourcePlaceholderConfigurer来加载yml文件,暴露yml文件到spring environment

// 加载YML格式自定义配置文件
	@Bean
	public static PropertySourcesPlaceholderConfigurer properties() {
		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
		yaml.setResources(new FileSystemResource("config.yml"));//File引入
//		yaml.setResources(new ClassPathResource("youryml.yml"));//class引入
		configurer.setProperties(yaml.getObject());
		return configurer;
	}
#config.yml
prefix:
  name:
  list:
    - name: tech
      key: 123
      source: beijing
    - name: skill
      key: 987
      source: shanghai

#在yml文件中,‘---’表示分隔符,表示多个yml,当Component在ConfigurationProperties注解的属性中prefix设置值后,list2无法直接加载为map,即如果你声明一个list2的map是没有值的,只能通过list2.name以字符串的形式获取到

---
your:
  username: test

---
list2:
  name: qwer
  url: http://blog.csdn.net/luckyrocks

yml语法

# 对象
# conf.yml
animal: pets
hash: { name: Steve, foo: bar }


{
    { "animal": "pets" },
    { "hash": { "name": "Steve", "foo": "bar" } }
}

# 数组
# conf.yml
Animal:
 - Cat
 - Dog
 - Goldfish

{ "Animal": [ "Cat", "Dog", "Goldfish" ] }


#null
# conf.yml
parent: ~

{ "parent": null }



 

转载于:https://my.oschina.net/u/3020519/blog/3066055

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值