• SpringBoot的配置文件properties和yml怎么写?


    开始看这篇文章之前,我先说明,我会重点讲解yml的配置,更加简单方便,但是语法要求较严格。

    注意:

    • properties的优先级比yml高
    • 一个项目只有一个配置文件。
    • 下面properties我简称P,yml简称Y

    端口配置

    P

    # 应用服务 WEB 访问端口
    server.port=8080
    
    • 1
    • 2

    Y

    server:
      port: 8080
    
    • 1
    • 2

    自定义配置

    假如需要一个密钥key才能调用学习api,那么key怎么配置,如何获取?

    • 定义学习api密钥(随便取名)

    P

    # 定义学习api密钥
    study.key=123456
    study.user=nickbears
    
    • 1
    • 2
    • 3

    Y

    # 定义学习api密钥
    study:
      key: 123456
      user: nickbears 
    
    • 1
    • 2
    • 3
    • 4

    如何读取配置文件?(为了效果明显,我用Bean生命周期里的afterPropertiesSet方法,为了在加载前读取配置进行打印操作)

    这是主函数:

    @SpringBootApplication
    public class Springboot02Application implements InitializingBean {
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02Application.class, args);
        }
    
        /**
         * 参数设置完成之后,执行此方法
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println();
            System.out.println("afterPropertiesSet:");
            System.out.println();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    读取方式 1 @Value

    @SpringBootApplication
    public class Springboot02Application implements InitializingBean {
        @Value("${study.key}") //读取配置文件
        private String studyKey;
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02Application.class, args);
        }
    
        /**
         * 参数设置完成之后,执行此方法
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println();
            System.out.println("afterPropertiesSet:");
            System.out.println(studyKey);
            System.out.println();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    注意:接下来我会省略以上部分代码,只保留核心代码。

    MYSQL配置

    • 没有对比没有伤害,谁简单?
    • 使用yml记得:后面一定要加一个空格
    • yml分层级写

    P

    # 设置MYSQL 的服务器连接信息
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/java100?characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456
    
    • 1
    • 2
    • 3
    • 4

    Y

    # 设置MYSQL 的服务器连接信息
    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/java100?characterEncoding=utf8
        username: root
        password: 123456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    yml读取不同数据类型

    # 测试数据读取不同数据类型
    # 测试数据读取不同数据类型
    Y
    myprofile:
      isdebug: true
    
    • 1
    • 2
    • 3
    • 4
    • 5

    读取

    @SpringBootApplication
    public class Springboot02Application implements InitializingBean {
        @Value("${study.key}") //读取配置文件
        private String studyKey;
    
        @Value("${myprofile.isdebug}")
        private boolean flag;
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02Application.class, args);
        }
    
        /**
         * 参数设置完成之后,执行此方法
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println();
            System.out.println("afterPropertiesSet:");
            System.out.println("studyKey:"+studyKey);
            System.out.println("isdbug:"+flag);
            System.out.println();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    读取方式2 @ConfigurationProperties

    • 我要读取一个对象

    Y

    # 读取对象
    user:
      id: 1
      name: nickbers
      age: 18
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建实体类

    • get 和 set 必须的,我用lombook的Data注解表示
    • @Component是必须的
    • @ConfigurationProperties 映射是必须的
    @Component //spring启动时将配置文件映射到当前类上
    @ConfigurationProperties("user")
    @Data //get 和上 set方法
    public class User {
        private int id;
        private String name;
        private int age;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    属性注入,打印查看效果

    @SpringBootApplication
    public class Springboot02Application implements InitializingBean {
        @Value("${study.key}") //读取配置文件
        private String studyKey;
    
        @Value("${myprofile.isdebug}")
        private boolean flag;
    
        @Autowired
        private User user;
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02Application.class, args);
        }
    
        /**
         * 参数设置完成之后,执行此方法
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println();
            System.out.println("afterPropertiesSet:");
            System.out.println("studyKey:"+studyKey);
            System.out.println("isdbug:"+flag);
            System.out.println("User:"+user);
            System.out.println();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    更多关于Spring配置项的可以去看官网学习,或者关注我的gitee,里面有详细的文档和代码供大家下载浏览,详情看我的第一篇文章,我的gitee 和 wiki文档

  • 相关阅读:
    《剑指offer》【剑指 Offer 12. 矩阵中的路径】
    leetcode:802. 找到最终的安全状态【反图 + 入度统计 + deque + 找出图中非环的节点】
    lightdb 普通用户拥有XMLTYPE类型的访问权限
    我的网站每个月给我带来了6W美元收入
    二分查找做的小游戏
    opencv人与摄像头距离检测
    狂神——SpringSecurity入门例子(设置不同用户访问权限)
    51单片机BH1750智能补光灯台灯光强光照恒流源LED控制系统
    《微服务设计》第三篇 :如何建模服务
    【计网】(六)传输层(TCP、UDP、可靠传输、流量控制......)
  • 原文地址:https://blog.csdn.net/weixin_50369395/article/details/126235886