项目结构

userPermission.yml

# 用户权限
user-permission:
api:
# 系统管理员
system_manager:
- "*:*:*" # 应用作者
app_author:
- "test:data:query"
- "test:data:add" # 应用用户
app_customer:
- "test:data:query"
- "test:data:add"

新建一个yml解析工厂 YmlConfigurationFactory.java

package cn.daenx.framework.YmlConfigurationFactory.config;

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; import java.io.IOException; public class YmlConfigurationFactory extends DefaultPropertySourceFactory { @Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
// 指定配置路径中的文件名
String filename = resource.getResource().getFilename() == null ? "" : resource.getResource().getFilename();
// 资源为空,则会抛出异常
resource.getInputStream();
if (resource == null || filename.endsWith(".properties")) {
// 配置文件为 properties 文件,调用原逻辑
return super.createPropertySource(name, resource);
} else if (filename.endsWith(".yml") || filename.endsWith(".yaml")) {
// yml文件 或 yaml文件,则使用yaml属性源加载器加载并返回标准属性源
return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource()).get(0);
} else {
// 非以上情况,则直接抛出异常,提示格式错误
throw new IOException("file format error! only support: properties, yml, yaml!");
}
}
}

新建你的映射bean UserPermissionProperties.java

package cn.daenx.framework.userPermission;

import cn.daenx.framework.YmlConfigurationFactory.config.YmlConfigurationFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import java.util.List;
import java.util.Map; @Data
@Configuration
@PropertySource(
value = "classpath:userPermission.yml",
ignoreResourceNotFound = true,
encoding = "UTF-8",
factory = YmlConfigurationFactory.class
)
@ConfigurationProperties(prefix = "user-permission")
public class UserPermissionProperties {
private Map<String, List<String>> api;
}

使用

    @Resource
private UserPermissionProperties userPermissionConfig; /**
* 测试接口
*
* @return
*/
@GetMapping("/test0")
public Result test0() {
Map<String, List<String>> api = userPermissionConfig.getApi();
System.out.println(api);
return Result.ok("查询成功");
}

springboot读取并映射额外的yml配置到bean的更多相关文章

  1. springboot application.yml配置学习

    一.背景 为了更好的使用springboot,所以看一下application.yml配置这块.主要是看数据绑定这块. 主要参考:https://www.hangge.com/blog/cache/d ...

  2. springboot 读取 yml 配置的几种方式

    前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...

  3. Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置

    从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...

  4. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  5. Springboot整合Spring Cloud Kubernetes读取ConfigMap,支持自动刷新配置

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Docker & Kubernetes相关文章:容器技术 之前介绍了Spring Cloud Config的用法,但 ...

  6. SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池

    三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...

  7. 每日笔记---使用@ConfigurationProperties读取yml配置

    每日笔记---使用@ConfigurationProperties读取yml配置 参考地址  https://www.cnblogs.com/mycs-home/p/8352140.html 1.添加 ...

  8. SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

    SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例 这是一个简单的SpringBoot整合实例 这里是项目的结构目录 首先是pom.xml ...

  9. SpringBoot的yml配置

    Spring Boot的yml配置 #开发配置 spring: data: solr: host: http://localhost:6789/solr/mote mvc: view: # 页面默认前 ...

  10. Springboot读取配置文件的两种方法

    第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...

随机推荐

  1. 【ABAQUS2023-Output Vars】使用记录

    计算结构的应变能,ALLSE=所有单元的ESEDEN*EVOL.但这不适用于模态分析,因为模态分析EVOL不能用 ALLSE Field: no History: yes .fil: automati ...

  2. 设备管理笔记1-oee

    什么是oee 类似于一种设备管理模型,如软件行业的质量模型.cmmi模型等 指标包括什么? 正常指标应该是多少,目前我们的指标为多少? 制造行业存在的6大问题分别是什么 指标包括什么? 包括 可用性性 ...

  3. JMeter 线程编号 __threadNum 获取不到

    场景: 在 BeanShell PreProcessor 中,使用 vars.get("__threadNum") 获取不到当前线程数,如: import org.apache.j ...

  4. nginx 简单实践:负载均衡【nginx 实践系列之四】

    〇.前言 本文为 nginx 简单实践系列文章之三,主要简单实践了负载均衡,仅供参考. 关于 Nginx 基础,以及安装和配置详解,可以参考博主过往文章: https://www.cnblogs.co ...

  5. 【WPF开发】 direct3d11 调试报错

    环境:VS2022 WPF Win11 过程:准备调试d3d11着色器转换nv12->rgb的过程 报错信息:DXGI_ERROR_SDK_COMPONENT_MISSING 应用程序请求的操作 ...

  6. 选择排序(LOW)

    博客地址:https://www.cnblogs.com/zylyehuo/ # _*_coding:utf-8_*_ def select_sort(li): for i in range(len( ...

  7. 使用ssh连接virtual Box里的虚拟机

    使用ssh连接virtual Box里的虚拟机 需求:virtual Box提供的文件拖放功能在从虚拟机拖向主机时,会出现一些卡顿,因此考虑使用ssh代替其文件传输功能. 高级 -> 端口转发 ...

  8. 学习EXTJS6(10)面向对象的基础框架-2【统一的组件模型】很重要

    用到ExtJS,UNIGUI是以ExtJS为基础的框架.因此掌握基础组件模型太重要了.确实就是让自己知道其所以然. Ext中所有可视组件都继承自Ext.Component. 1.Ext.Compone ...

  9. jeecgboot前端按钮角色权限控制(是否隐藏)

    官方文档 http://doc.jeecg.com/2044038 解决办法 首先需要修改前端代码,在想获得权限控制的按钮组件中使用指令 v-has="''". 代码示例: < ...

  10. rabbitmq分布式事务

    Rabbitmq解决分布式事务的原理:可靠消息最终一致性方案 需要保证以下三要素 1.确认生成者一定要将消息可靠投递到MQ[采用MQ的异步confirm消息确认机制] public interface ...