• 04-SpringBoot的基础配置及其配置文件分类,解决Yaml文件失效问题


    SpringBoot的配置

    SpringBoot是用来提高Spring程序的开发效率的,使用SpringBoot后几乎不用做任何配置功能就有了,因为很多功能已经有默认配置帮我们做好了

    配置文件的相关配置

    在一个项目中不同的技术对应不同的配置文件并且这些配置文件的格式也不统一

    SpringBoot提供了一个专门的配置文件application.properties修改所用技术的配置(如服务器或数据库的配置),既统一了配置文件的格式也便与集中管理

    • resources/application.properties文件中只要输入要配置的属性关键字就可以根据IDE的提示进行设置
    # 设置访问tomcat的端口号(默认是8080)
    # 如果不使用Spring Boot,修改端口号就需要在tomcat服务器的配置文件中改 
    server.port=80
    # 关闭运行日志图表
    spring.main.banner-mode=off
    # 设置运行日志的显示级别
    logging.level.root=debug
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在SpringBoot官方文档附录中的Application Properties可以获取到不同技术相关的配置项,但只有导入了对应starter后SpringBoot才会提供对应的配置属性提示

    在这里插入图片描述

    spring-boot-starter是SpringBoot中所有starter的基础依赖,里面定义了SpringBoot工程相关的基础配置项

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
        <version>2.5.4version>
        <scope>compilescope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    配置文件分类

    SpringBoot工程中的配置文件支持三种配置格式

    • properties格式: 配置项属性写起来代码冗余
    • yml(主流)和yaml格式: 这两种格式除了文件名后缀不一样,文件格式完全一样

    配置文件加载优先级: application.properties > application.yml > application.yaml

    #application.properties(properties格式)
    server.port=80
    
    #application.yml(yml格式)
    server:
      port: 81
      
    #application.yaml(yaml格式)
    server:
      port: 82
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置文件可以有多个: 不同配置文件中相同配置属性按照加载文件的优先级相互覆盖,不同配置文件中不同配置属性全部保留即所有的配置项都会生效

    #application.properties(properties格式)
    server.port=80
    spring.main.banner-mode=off
    
    #application.yml(yml格式)
    server:
      port: 81
    logging: 
      level: 
        root: debug
    #application.yaml(yaml格式)
    server
      port: 82
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    配置文件自动提示失效问题

    配置文件自动提示功能是IDEA这个编程工具给我们提供的并不是SpringBoot技术给我们提供的,自动提示功能消失的大体原因有2种

    • 第一种现象: IDEA认为你现在写配置的文件不是个配置文件,所以拒绝给你提供提示功能
    • 第二种现象: IDEA认定你是合理的配置文件,但是IDEA加载不到对应的提示信息

    第一步: Files→Project Structure, 在Facets选项右侧的Spring下选择配置文件自动提示功能消失的那个SpringBoot工程

    在这里插入图片描述

    第二步: 点击Customize Spring Boot按钮,查看当前SpringBoot工程中所有的配置文件

    在这里插入图片描述

    第三步: 点击+号在当前工程下添加没有自动提示功能的配置文件

    在这里插入图片描述

    自定义类绑定的配置提示

    自定义的类和配置文件绑定时一般没有提示, 若要提示添加如下依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-configuration-processorartifactId>
        <optional>trueoptional>
    dependency>
    
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.bootgroupId>
                            <artifactId>spring-boot-configuration-processorartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    动态规划---打家劫舍问题
    【微机接口】第四章:宏指令程序设计
    Linux 服务器使用过程中,mysql突然不能用了,报如下错误
    视频编辑软件Premiere Pro 2022 mac(pr2023)v22.6.2中文功能
    Docker网络模式和数据管理
    【SSM框架】Mybatis详解08(源码自取)之动态sql详解
    AlibabaP9整理出微服务笔记:Spring微服务不止架构和设计
    C++ - 8月31日 - 约瑟夫环问题
    机器学习——主成分分析(PCA,未完)
    C++ Reference: Standard C++ Library reference: C Library: cstdio: ftell
  • 原文地址:https://blog.csdn.net/qq_57005976/article/details/134257964