Spring Boot2

SpringBoot 配置文件

properties配置文件

application.properties

以配置端口和访问路径为例


server.port=8080

yaml配置文件

application.yml / application.yaml

server:
  port: 81

在实际开发中,更常用的是yaml配置文件

yaml层级表示更加明显

yml配置信息书写与获取

lesson: SpringBoot

enterprise:
  name: itcat
  age: 16
  tel: 4000161933
  subject:
    - java
    - 前端
    - 大数据

yml书写注意事项:

        值前边必须有空格,作为分隔符

        使用空格作为缩进表示层级关系,相同的层级左对齐

获取

    //yaml文件数据读取1
    @Value("${lesson}")
    private String lesson;

    @Value("${enterprise.subject[0]}")
    private String subject_00;
    //yaml文件数据读取2
    @Autowired
    private Environment environment;
    //yaml文件数据读取3
    @Autowired
    private Enterprise enterprise;

方式三实体类如下

//封装yaml对象格式数据必须先声明当前实体类受Spring管控
@Component
//使用@ConfigurationProperties注解定义当前实体类读取配置属性信息,通过prefix属性设置读取哪个数据
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;

相关推荐

  1. springboot引入swagger2

    2024-03-24 15:04:04       32 阅读
  2. springboot 整合 Caffine(springboot3.2

    2024-03-24 15:04:04       17 阅读
  3. SpringBoot2升级到SpringBoot3总结

    2024-03-24 15:04:04       15 阅读
  4. SpringBoot整合 R2DBC

    2024-03-24 15:04:04       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 15:04:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 15:04:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 15:04:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 15:04:04       20 阅读

热门阅读

  1. FFmpeg滤波器创建

    2024-03-24 15:04:04       16 阅读
  2. Uniapp manifest

    2024-03-24 15:04:04       16 阅读
  3. 搜索旋转排序数组

    2024-03-24 15:04:04       22 阅读
  4. 课时73:流程控制_for循环_综合案例

    2024-03-24 15:04:04       16 阅读
  5. 【Linux】调试器-gdb的安装与使用

    2024-03-24 15:04:04       16 阅读
  6. dom元素+CSS实现阶梯动画效果

    2024-03-24 15:04:04       17 阅读
  7. Linux安装iptables 防火墙

    2024-03-24 15:04:04       19 阅读
  8. 前端安全之XSS与CSRF

    2024-03-24 15:04:04       14 阅读
  9. Median of an Array(贪心策略,编程技巧)

    2024-03-24 15:04:04       19 阅读
  10. Mysql

    Mysql

    2024-03-24 15:04:04      15 阅读
  11. arm linux应用程序crash分析一般方法

    2024-03-24 15:04:04       17 阅读
  12. Vue2 CSS小知识点

    2024-03-24 15:04:04       18 阅读