Springboot如何获取yml、properties参数
作者:观海听涛丶 发布时间:2021-09-20 17:34:43
标签:Springboot,yml,properties,参数
如何获取yml、properties参数
1、使用@Value()注解
1.1 配置数据
如:在properties.yml文件配置如下数据
message_zh: 张三
message_en: ergouzi
在controller中获取:
1.2 读取数据
读取自定义文件:须加注解
@PropertySource(value = {"classpath:config.yml","classpath:config.properties"})
读取application文件不需要加注解
// 中文
@Value("${message_zh}")
private String message_zh;
// 英文
@Value("${message_en}")
private String message_en;
@RequestMapping(value = "/{id}")
public String index(HttpServletRequest request, @PathVariable Integer id){
? ? if (id == 1 ){
? ? ? ? request.setAttribute("info",message_zh);
? ? }else {
? ? ? ? request.setAttribute("info", message_en);
? ? }
? ? return "index";
}
2、使用 @component
@ConfigurationProperties(prefix = "user")
@PropertySource(value = "classpath:myConfig.properties")
首先在myConfig.properties或myConfig.yml中配置参数:
user.userName = '李二狗'
user.password = 'admin'
2.1 javabean
/**
?* 〈一句话功能简述〉<br>?
?* 〈yml或properties配置参数〉
?*
?* @author 丶Zh1Guo
?* @create 2018/11/21
?* @since 1.0.0
?*/
@Component// 组件
@ConfigurationProperties(prefix = "user")? // 前缀
@PropertySource(value = "classpath:myConfig.properties")// 自定义配置文件路径
public class properConfig {
? ? private String userName;// 注意要和配置文件一致
? ? private String password;
? ? public String getUserName() {
? ? ? ? return userName;
? ? }
? ? public void setUserName(String userName) {
? ? ? ? this.userName = userName;
? ? }
? ? public String getPassword() {
? ? ? ? return password;
? ? }
? ? public void setPassword(String password) {
? ? ? ? this.password = password;
? ? }
}
2.2 controller
/**
?* 〈一句话功能简述〉<br>?
?* 〈〉
?*
?* @author 丶Zh1Guo
?* @create 2018/11/21
?* @since 1.0.0
?*/
@restController
public class template {
? ? @Autowired
? ? properConfig config;
? ? @RequestMapping(value = "/config")
? ? public String config(){
? ? ? ? return config.getUserName();
? ? }
}
总结:
第一种方法适合只取某些数据
第二种方法适合取所有数据
yml和properties区别
yml:key:(空格)value
properties: key = value
配置文件读取yml自定义参数(亲测可用)
dict:
? js:
? ? url: D:\jsFile\
首先自定义一个参数
@Component
@Data
@ConfigurationProperties(prefix = "dict.js")
@PropertySource(value = "classpath:application-dev.yml")
public class PropertisParam {
? ? private String url;
}
利用平时@value 获取值
然后在所需要的调用的配置类里面注入PropertisParam,利用@PostConstruct初始化值
@Resource
private PropertisParam param;
private static String root=null;
@PostConstruct
public void init(){
? ? root = param.getUrl();
}
另一种方式
@Data
@Component
@ConfigurationProperties(prefix = "spring")
public class LoginBody {
? ? private String appid;
? ? private String apiCode;
? ? private String userName;
}
基本写法就不解释了:主要讲一哈注入方式
类上面添加@component
private static LoginBody loginBody;
@Resource
public void init(LoginBody loginBody) {
? ? SecurityUtil.loginBody = loginBody;
}
来源:https://www.cnblogs.com/wangzh1guo/p/9995248.html


猜你喜欢
- 最新开发新项目的时候,要做分享项目,要求分享有微信,微信朋友圈,QQ,QQ空间,新浪微博这五个,所分享内容包括,分享纯图片,纯文字,图文类型
- spring in action第三版读书笔记spring3.0引入了spring expression language(spel)语言,
- 注解版步骤新建一个module,添加web的支持由于Maven可能存在资源过滤的问题,我们将配置完善pom.xml<build>
- //写注册表RegistryKey regWrite;//往HKEY_CURRENT_USER主键里的Software子键下写一个名为“Te
- (1)实际应用BeanUtils.copyProperties(赋值目标对象,模板源对象);我们都知道当有两个对象AB,属性名称一样的情况下
- 开发Android APP微信支付功能,需要完成三个步骤:第一步生成预支付订单、第二步生成微信支付参数、第三步调起微信APP支付。除了需要审
- 什么是链表?链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针连接次序实现的。每一个链表都包含多个节点,
- 一、什么是封装?封装就是将属性私有化,提供公有的方法访问私有属性。做法就是:修改属性的可见性来限制对属性的访问,并为每个属性创建一对取值(g
- 采用继承Thead类实现多线程:优势:编写简单,如果需要访问当前线程,只需使用this即可,无需使用Thead.currentThread(
- 原因分析@Anysc注解会开启一个新的线程,主线程的Request和子线程是不共享的,所以获取为null在使用springboot的自定带的
- list.remove最近做项目的过程中,需要用到list.remove()方法,结果发现两个有趣的坑,经过分析后找到原因,记录一下跟大家分
- 前言请看下面几个问题Spring为什么不推荐使用@Autowired 注解?为什么推荐使用@Resource 代替&nb
- 前言含义:(1)多重循环指一个循环语句的循环体中再包含循环语句,又称嵌套循环。(2)循环语句内可以嵌套多层循环。(3)不同的循环语句可以相互
- 本文实例为大家分享了java微信公众号支付示例代码,供大家参考,具体内容如下开始之前,先准备好:appid、商家号、商户密匙。工具类:MD5
- 前言Postman是一款Http请求模拟工具.它可以模拟各种Http Request,使用起来十分的方便.使用背景利用Spring Boot
- 汉诺塔游戏一旦掌握了规律,其实是有点单调和无聊的,不过却是学习递归的一个绝佳例子,想当初学习老谭C的时候,就卡在这儿好长时间。对初学编程的人
- 前言在我们开发过程中,由于主流的架构都是采用前后端分离的方式,我们作为后端开发者需要为前段持续地提供运行在容器中最新代码,虽然可
- 背景:新年之际,微信微博支付宝红包是到处飞,但是,自己的手速总是比别人慢一点最后导致红包没抢到,红包助手就应运而生。需求:收到红包的时候进行
- swing自带的窗体是不能够满足我们的应用需求的,所以需要制作任意图片和形状的JFrame框体,比如下图:并且可以设置窗体背景图片的透明度下
- 这篇文章主要介绍了MyBatis执行Sql的流程实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的