【SpringBoot3.x教程02】SpringBoot配置文件详解

前言:什么是配置文件

SpringBoot的配置文件是指用于定义和管理SpringBoot应用程序配置的文件。这些配置文件允许开发者调整和控制应用程序的行为,而无需改变代码。主要有两种格式的配置文件:
1、application.properties:这是一种使用简单键值对来配置的传统格式。它位于项目的src/main/resources目录下。每一项配置都是一行,以点分隔的属性名和其值组成,例如:

server.port=8080
spring.datasource.url=jdbc:mysql://localhost/test

2、application.yml:这是一种使用YAML(YAML Ain’t Markup Language)格式的配置文件,也位于src/main/resources目录下。YAML是一种数据序列化格式,以数据的层次结构为基础,适合表示复杂的配置结构。YAML文件的读取顺序是从上到下,支持使用缩进表示层级关系,YAML文件是强格式的,对于缩进有着严格的要求,每次缩进两格代表一个属性,例如:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost/test

本文重点:配置文件中的常用配置

1. 基本属性配置

application.propertiesapplication.yml中配置应用的基本属性,比如端口、上下文路径等。

  • application.properties

    server.port=8080
    server.servlet.context-path=/app
    
  • application.yml

    server:
      port: 8080
      servlet:
        context-path: /app
    

2. 数据库配置

配置数据源,包括URL、用户名和密码等。

  • application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/mydb
    spring.datasource.username=root
    spring.datasource.password=secret
    
  • application.yml

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/mydb
        username: root
        password: secret
    

3. 日志配置

配置日志级别和日志文件路径。

  • application.properties

    logging.level.root=WARN
    logging.file.name=app.log
    
  • application.yml

    logging:
      level:
        root: WARN
      file:
        name: app.log
    

4. 多环境配置

使用不同的配置文件管理不同环境的配置。

  • application-dev.properties

    server.port=8081
    
  • application-prod.properties

    server.port=80
    

激活特定环境的配置文件。

  • application.properties

    spring.profiles.active=dev
    
  • 通过命令行参数激活

    java -jar yourapp.jar --spring.profiles.active=prod
    

5. 自定义配置

定义自己的配置属性并在应用中引用。

  • application.properties

    app.custom.property=MyValue
    
  • 使用@Value注解引用

    @Value("${app.custom.property}")
    private String customProperty;
    

6. 配置文件中的占位符

使用已有的配置值作为其他配置值的一部分。

  • application.properties

    app.name=MyApp
    app.description=${app.name} is a SpringBoot application
    

7. 配置绑定到实体类

将配置文件的属性绑定到一个实体类上,支持各种类型数据的配置,比如List和Map。

  • application.yml

    app:
      name: MyApp
      description: My SpringBoot application
      fruits:
        - apple
        - banana
        - orange
      fruitColors:
        apple: red
        banana: yellow
        orange: orange
    
  • 实体类

    @Component
    @ConfigurationProperties(prefix="app")
    public class MyAppProperties {
        private String name;
        private String description;
        // getters and setters
    }
    

代码截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

结束语:

本文章讲述了配置文件中常用的一些配置内容,并给出了对应的代码示例,对应的代码会上传到github,下一节我们主要讲解SpringBoot的自动配置

  • 29
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[8\]:pom.xml文件详解。pom.xml文件是Maven项目的核心配置文件,用于管理项目的依赖、插件、构建配置等。在Spring Boot项目中,pom.xml文件也扮演着重要的角色。在pom.xml文件中,可以配置项目的资源文件。 在Spring Boot的pom.xml文件中,通常会有一个<resources>标签,用于配置项目的资源文件。资源文件可以包括静态文件、配置文件等。在<resources>标签中,可以使用<resource>标签来指定资源文件的路径和过滤规则。 例如,可以使用以下配置来指定资源文件的路径和过滤规则: ```xml <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> ``` 上述配置中,<directory>标签指定了资源文件的路径为src/main/resources,<includes>标签指定了需要包含的资源文件的过滤规则。在这个例子中,所有的.properties和.xml文件都会被包含在项目的资源文件中。 通过配置pom.xml文件中的<resources>标签,可以方便地管理项目的资源文件,使其能够在项目中正确地被加载和使用。 #### 引用[.reference_title] - *1* *3* [SpringBoot入门pom.xml文件详解,Resources目录结构详解Springboot创建项目,注解详解等等,博主为很少人](https://blog.csdn.net/m0_52789121/article/details/124038257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot入门pom.xml文件详解,Resources目录结构详解Springboot创建项目,注解详解等等,博主为很少人...](https://blog.csdn.net/qq_38101814/article/details/91044906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

升仔聊编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值