spring-boot加载自定义yml配置文件

说明

这个只是本人自己学习时做的笔记,比较基础,希望大佬不喜勿喷。
有需要的小伙伴可以参考一下,只是很基础的笔记。谢谢大家!

书写yml配置文件

放在类路径(classpath)下:
在这里插入图片描述
语法代码:

logistics:
   permisssion: 
      data-checked: false
      remote-checked: false

书写自定义yml工厂类

实现代码:

package com.***.logistics.infrastructure.utils;

import java.io.IOException;
import java.util.List;

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

/**
 * 
 * @author wuqh
 * @date 2019年9月25日 下午2:35:20
 * @Description
 * @version 1.0
 */
public class YamlConfigFactory extends DefaultPropertySourceFactory {

	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(),
				resource.getResource());
		return sources.get(0);
	}
}

书写使用实现类

代码实现:

package com.***.logistics.infrastructure.config;

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;

import com.***.logistics.infrastructure.utils.YamlConfigFactory;

/**
 * @author wuqh
 * @date 2019年9月25日 上午9:33:37
 * @Description
 * @version 1.0
 */
@Component
@PropertySource(value = { "classpath:logistics-permission.yml" }, factory = YamlConfigFactory.class)
@ConfigurationProperties(prefix = "logistics")
public class LogisticsPermissionConfig {

	@Value("${permisssion.data-checked:true}")
	private boolean dataChecked;

	@Value("${permisssion.remote-checked:true}")
	private boolean remoteChecked;

	/**
	 * 是否要进行数据权限校验
	 * 
	 * @return boolean true是、false否
	 */
	public boolean isDataChecked() {
		return dataChecked;
	}

	/**
	 * 是否要进行远程权限校验
	 * 
	 * @return boolean true是、false否
	 */
	public boolean isRemoteChecked() {
		return remoteChecked;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值