说明
这个只是本人自己学习时做的笔记,比较基础,希望大佬不喜勿喷。
有需要的小伙伴可以参考一下,只是很基础的笔记。谢谢大家!
书写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;
}
}