• Spring 配置


    配置文件最主要的目的 : 解决硬编码的问题(代码写死)

    SpringBoot 的配置文件,有三种格式

    1.properties 

    2.yaml

    3.yml(是 yaml 的简写)

    SpringBoot 只支持三个文件 

    1.application.properties

    2.application.yaml

    3.application.yml

    yaml 和 yml 是一样的,学会一个就行

    如果一个项目中同时存在 properties 和 yml ,虽然两个都会生效,但是 properties 的优先级更高

    但是正常情况下都只有一个文件,多了容易乱

    properties 的代码格式一般为键值对的样式,以 = 分割,单词小写,单词之间用 . 分割

    下面是一些配置举例,配置端口号和配置数据库先不细说

    我们自定义的配置该如何拿到运用呢?

    我们创建一个类

    1. package com.example.ioc.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class PropertiesController {
    7. //读取配置文件
    8. @Value("${demo.key1}")
    9. private String key1;
    10. @RequestMapping("/readkey")
    11. public String readkey(){
    12. return "读取到的配置项key1"+key1;
    13. }
    14. }

    就能成功拿到了

    名字一一对应

    什么样的内容适合放在配置文件中呢?

    那些可能会发生改变的信息,与我的程序运行没有太大关系的,我们就把它放在配置文件中

    但是其实我们发现 properties 有很多冗余的信息 比如

    想要解决这个问题,就可以使用 yml 配置文件的格式化了

    我们先在 resources 底下创建一个文件,application.yml

    yml 文件对比 properties 文件格式,yml 文件把 . 换成冒号+换行,key后面用冒号赋值

    这样就可以把端口改为 9090 了

    但是我们稍作修改,把9090 前面的空格删掉,再次运行程序,发现修改端口失败了

    yml 的格式有严格要求,我们要在值前面的冒号的后面加空格,空格不可省略

    我们对数据库相关配置进行修改的样式如下

    yml 的自定义配置该如何写并且使用呢?

    然后创建一个类

    1. package com.example.ioc.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. public class YmlController
    7. {
    8. @Value("${demo.key1}")
    9. public String key1;
    10. @RequestMapping ("/readYml")
    11. public String readYml(){
    12. return key1;
    13. }
    14. }

    这样就能成功使用了

    我们再看看多个数据

     @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法

    1. package com.example.ioc.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import javax.annotation.PostConstruct;
    6. @RestController
    7. public class YmlController
    8. {
    9. @Value("${demo.key1}")
    10. public String key1;
    11. @Value("${demo.key2}")
    12. public String key2;
    13. @Value("${demo.key3}")
    14. public String key3;
    15. @RequestMapping ("/readYml")
    16. public String readYml(){
    17. return key1;
    18. }
    19. @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法
    20. public void init(){
    21. System.out.println("key1:"+key1);
    22. System.out.println("key2:"+key2);
    23. System.out.println("key3:"+key3);
    24. }
    25. }

    我们再看看单双引号的区别

    双引号里面的 \n 是换行

    单引号会对特殊字符进行转义,因为\n 本身表示的意思是换行,但是使用单引号的时候,内容变成了 \n 而不是换行,所以认为是转义

    yml 该如何配置对象?

    配置文件为

    再创建一个student类

    1. package com.example.ioc;
    2. import lombok.Data;
    3. import org.springframework.boot.context.properties.ConfigurationProperties;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. @ConfigurationProperties(prefix = "student")
    7. @Data
    8. public class Student {
    9. private Integer id;
    10. private String name;
    11. private Integer age;
    12. }

    然后就能运行了 

    1. package com.example.ioc.controller;
    2. import com.example.ioc.Student;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.beans.factory.annotation.Value;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import javax.annotation.PostConstruct;
    8. @RestController
    9. public class YmlController
    10. {
    11. @Autowired
    12. public Student student;
    13. @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法
    14. public void init(){
    15. System.out.println("student:"+student);
    16. }
    17. }

    yml 如何配置集合呢?

    1. package com.example.ioc.model;
    2. import lombok.Data;
    3. import org.springframework.boot.context.properties.ConfigurationProperties;
    4. import org.springframework.stereotype.Component;
    5. import java.util.List;
    6. @Component
    7. @ConfigurationProperties(prefix = "dbtypes")
    8. @Data
    9. public class DBTypes {
    10. private List name;
    11. }

     

    1. package com.example.ioc.controller;
    2. import com.example.ioc.model.DBTypes;
    3. import com.example.ioc.model.Student;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.beans.factory.annotation.Value;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.annotation.PostConstruct;
    9. @RestController
    10. public class YmlController
    11. {
    12. @Autowired
    13. public DBTypes dbTypes;
    14. @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法
    15. public void init(){
    16. System.out.println("dbTypes:"+dbTypes);
    17. }
    18. }

    用数组去接收也是可以滴

    记得要加空格哦

    yml 也可以配置map

    1. import lombok.Data;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.stereotype.Component;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. @Component
    7. @ConfigurationProperties(prefix = "dbtypes")
    8. @Data
    9. public class DBTypes {
    10. private List name;
    11. private HashMap map;
    12. }
    1. package com.example.ioc.controller;
    2. import com.example.ioc.model.DBTypes;
    3. import com.example.ioc.model.Student;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.beans.factory.annotation.Value;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.annotation.PostConstruct;
    9. @RestController
    10. public class YmlController
    11. {
    12. @Autowired
    13. public DBTypes dbTypes;
    14. @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法
    15. public void init(){
    16. System.out.println("dbTypes:"+dbTypes);
    17. }
    18. }

    yml 的优缺点 :

  • 相关阅读:
    ipynb转换为pdf、Markdown(.md)
    Linux more 命令使用介绍
    MySQL 事务隔离级别和MVCC版本控制
    Steam下载MOD至本地文件夹
    阅读llama源码笔记_1
    【杰理AC696X】外挂收音IC RDA5807的频段设置
    【数据结构】单链表定义的介绍及增删查改的实现
    java面试官如何面试别人
    btc钱包探索纪实
    ElasticSearch学习(二): Mapping的数据类型和参数
  • 原文地址:https://blog.csdn.net/qq_40841463/article/details/134486148