当前位置: 首页 > news >正文

springboot的配置信息的设置和读取(application.properties/application.yml)

springboot提供了两种配置信息的文件格式,application.properties和application.yml,基于直接明了,使用方便和高效的前提下下面的配置均采用yml格式配置,

注意

  1. yml采用缩减方式来排列
  2. 键后面紧跟冒号,然后空格,最后是值
  3. 注意里面用到爱好数字的配置信息每个数组值前面的-(横杠)和值之间有一个空格

properties也yml/ymal相互转换

在线yaml转properties-在线properties转yaml-ToYaml.com在线yaml转properties工具 - toyamlicon-default.png?t=N7T8https://www.toyaml.com/

配置信息的分类(三种)

具体可以查看官网

Spring Boot Reference Documentationicon-default.png?t=N7T8https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#legal

springboot标准的配置

例如web服务的端口和上下文路径

server:port: 9999servlet:context-path: /yuanma

其他第三方配置信息

例如集成redis

spring:data:redis:host: "localhost"port: 6379database: 0username: "user"password: "secret"

自定义配置信息(两种获取信息的方式)

@Value+EL表达式

例如下面配置了邮件信息

#邮件信息
mail:from: 123@qq.comto: 456@qq.comserver: smtp.qq.comport: 8888

下面用@Value+EL表达式配置

package com.burns.yuanma.admin.pojo;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@Data
public class MailProperties {/*** 发件人*/@Value("${mail.from}")private String from;/*** 收件人*/@Value("${mail.to}")private String to;/*** 服务器地址*/@Value("${mail.server}")private String server;/*** 服务器端口号*/@Value("${mail.port}")private String port;
}

@ConfigurationProperties+prefix(前缀)

例如下面的用户信息配置,

#用户信息
user:address: 北京市朝阳区安外大羊坊八号院8号楼2单元112name: 张三age: 12sex: 男hobbies:- 足球- 游泳- 骑行- 篮球- 棒球

获取用户信息

package com.burns.yuanma.admin.pojo;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "user")
@Data
public class UserProperties {/*** 姓名*/private String name;/*** 年齡*/private int age;/*** 性別*/private String sex;/*** 郵件*/private String email;/*** 住址*/private String address;/*** 愛好*/private String[] hobbies;
}

访问接口获取信息

上面两种方式都可以获取信息

下面是controller

package com.burns.yuanma.admin.controller;import com.burns.yuanma.admin.pojo.MailProperties;
import com.burns.yuanma.admin.pojo.UserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class PropertiesController {@Autowiredprivate UserProperties userProperties;@Autowiredprivate MailProperties mailProperties;@RequestMapping("/userInfo")public String userInfo(){System.out.println(userProperties.toString());return userProperties.toString();}@RequestMapping("/mailInfo")public String mailInfo(){System.out.println(mailProperties.toString());return mailProperties.toString();}}

访问浏览器

 用户信息

http://localhost:9999/yuanma/userInfoicon-default.png?t=N7T8http://localhost:9999/yuanma/userInfo

 邮件信息

http://localhost:9999/yuanma/mailInfoicon-default.png?t=N7T8http://localhost:9999/yuanma/mailInfo

 

http://www.lryc.cn/news/227357.html

相关文章:

  • Deepsort项目详解
  • C语言证明一个偶数总能表示为两个素数之和。输入一个偶数并将其分解为两个素数
  • Python 的 datetime 模块
  • Termius for Mac:掌控您的云端世界,安全高效的SSH客户端
  • Ubuntu 下监控并自动重启网卡
  • 377. 组合总和 Ⅳ
  • 【OpenCV】计算视频的光流并跟踪物体calcOpticalFlowPyrLK
  • C语言进阶
  • Linux之gdb
  • 100天精通风控建模(原理+Python实现)——第3天:风控建模中如何处理缺失值?
  • Leetcode—680.验证回文串II【简单】
  • Redis五种数据类型及命令操作(二)
  • 低代码信创开发核心技术(三):MDA模型驱动架构及元数据系统设计
  • HslCommunication模拟西门子读写数据
  • 多测师肖sir_高级金牌讲师_ui自动化po框架版本02
  • 线性判别分析(Linear Discriminant Analysis,LDA)
  • git的分支及标签使用及情景演示
  • 深度解析找不到msvcp120.dll相关问题以及解决方法
  • SQL Server 2022 安装步骤——SQL Server设置身份验证教程
  • Maven各方面配置好了却无法显示版本
  • Jdk 1.8 for mac 详细安装教程(含版本切换)
  • 02MyBatisPlus条件构造器,自定义SQL,Service接口
  • c语言练习11周(6~10)
  • 钉钉API与集简云无代码开发连接:电商平台与营销系统的自动化集成
  • C++算法:包含三个字符串的最短字符串
  • 华为开源carbondata中的使用问题处理
  • AI:76-基于机器学习的智能城市交通管理
  • 区块链游戏,游戏开发
  • 单片机程序无法下载?
  • 【数据库】【sql】如何用SQL实现跨行计算