前言

为了 Spring Boot 能够更好地生成配置元数据文件,我们可以在创建项目时添加 Spring Configuartion Processor 依赖,或者在创建好项目后的 pom.xml 文件中手动添加。添加该依赖后,我们在编写配置时就会有属性提示,大大降低编写错误。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>

application.properties

自定义属性

application.properties 配置文件是创建项目后就自带的,如果我们要自定义属性,可以在其中直接配置,配置过程如下:

  1. application.properties 中添加我们要自定义的配置;
cunyu.id=1024
cunyu.name=村雨遥
cunyu.website=https://cunyu1943.github.io
  1. 创建实体类来映射我们配置的属性;
package com.cunyu.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @author : cunyu* @version : 1.0* @className : CunyuProperties* @date : 2020/7/29 13:34* @description : TODO*/@Component
@ConfigurationProperties(prefix = "cunyu")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CunyuProperties {private int id;private String name;private String website;
}
  1. 定义 Controller 来注入测试;
package com.cunyu.controller;import com.cunyu.pojo.CunyuProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author : cunyu* @version : 1.0* @className : CunyuPropertiesController* @date : 2020/7/29 13:37* @description : TODO*/@RestController
@RequestMapping("/cunyu")
public class CunyuPropertiesController {private static final Logger logger = LoggerFactory.getLogger(CunyuPropertiesController.class);@AutowiredCunyuProperties cunyuProperties;@GetMapping("/profile")public String cunyuProfile(){logger.info("---------------");logger.info(cunyuProperties.toString());logger.info("---------------");return cunyuProperties.toString();}
}
  1. 打开网页测试,打开 1,同时观察控制台,显示如下内容则说明属性注入成功;

多环境配置

实际开发过程中,常常需要多个环境(如 开发、测试、生产等),而不同环境的配置都不一样,此时配置方法如下;

  1. 创建不同环境对应的配置文件,配置文件名为 application-{profile}.properties{profile} 为我们自定义环境,如下:

开发环境:application-dev.properties

server.servlet.context-path=/dev

测试环境:application-test.properties

server.servlet.context-path=/test

生产环境:application-prod.properties

server.servlet.context-path=/prod
  1. 然后在 application.properties 中加入激活的环境,此时就会激活对应环境的配置;
# {profile} 对应上述的 dev、test、prod
spring.profiles.active={profile}

之所以要分为多个环境的配置,主要是方便在不同环境中开发的需求,比如我们要开发新功能,那此时就可以激活开发配置文件的相关设置,等待我们开发完成之后,然后再切换到测试环境进行测试。而经过严格的测试之后,我们就可以将新推出的功能上线到生产环境中。纵观整个开发流程,我们既完成了新功能的开发,也没有影响到用户对现有系统的使用,所以现在大家基本都是基于这种模式来进行业务开发。

自定义配置文件

假如我们不想用项目自带的 application.properties 配置环境,那我们也可以自定义我们需要的配置。但该如何配置呢?接下来我们就来看看 ~

  1. 首先创建一个自定义配置文件 my.properties,文件名可以自定义,但是后缀要保持一致,然后在其中加入我们自定义配置的属性;
my.id=1024
my.name=村雨遥
my.website=https://cunyu1943.github.io
  1. 定义实体类,用于映射自定义配置文件中的内容;
package com.cunyu.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** @author : cunyu* @version : 1.0* @className : MyProperties* @date : 2020/7/29 14:05* @description : TODO*/@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyProperties {private int id;private String name;private String website;
}
  1. 定义 Controller 来注入测试
package com.cunyu.controller;import com.cunyu.pojo.MyProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author : cunyu* @version : 1.0* @className : MyPropertiesController* @date : 2020/7/29 14:07* @description : TODO*/@RestController
@RequestMapping("/my")
public class MyPropertiesController {private static final Logger logger = LoggerFactory.getLogger(MyPropertiesController.class);@AutowiredMyProperties myProperties;@GetMapping("/profile")public String myProfile() {logger.info("=============");logger.info(myProperties.toString());logger.info("=============");return myProperties.toString();}
}
  1. 打开网页测试,打开 http://localhost:8080/my/profile,同时观察控制台,显示如下内容则说明属性注入成功;

注意

application.propertiesmy.properties 会优先加载 application.properties

.yml 和 .properties

一般来说,使用 IDEA 创建一个 Spring Boot 项目时,默认都会生成一个 application.properties 的配置文件。该配置文件是用来 修改 Spring Boot 自动配置的默认值。 但有的朋友会更倾向于使用 application.yml,那么问题来了,这两种格式到底有啥区别呢?

开始比较之前,我们先来看看各自的实例:

  • .properties 格式
server.port=8081
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.url=jdbc:mysql://aliyuncs.com:3306/database?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  • .yml 格式
server:port: 8082
spring:datasource:name: testurl: jdbc:mysql://127.0.0.1:3306/databaseusername: rootpassword: ******type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driver

从上面的实例我们可以发现,两者的区别主要有以下几点:

  1. 语法结构
  • .properties 格式使用的是 键值对形式(key=value),而 .yml 格式则使用的是 树状结构(key: value)

  • .properties 格式通过 . 来连接,= 来赋值,结构上比较直接,而 .yml 格式则使用 : 来分层,结构上呈现树状结构,层次感明显,而且赋值时 : 的后边必须 接着一个空格再赋值

  1. 执行先后顺序

如果一个工程中同时存在两种格式的文件,那么会 优先加载 .yml 文件,然后再加载 .properties,而且后加载的 .properties 会覆盖之前加载的 .yml 文件

此外,.yml 配置时需要注意以下几点:

  1. 缩进必须用空格,不能用 Tab
  2. @PropertySource 注解不能加载 yml 文件

总结

以上就是关于 Spring Boot 中的配置相关内容了。本文主要介绍了 Spring Boot 项目自带的配置文件的相关信息,同时也介绍了如果我们想要满足自己需求如何进行自定义配置。最后,则是对 .yml.properties 不同格式的配置文件的区别进行解释。

Spring Boot 配置详解相关推荐

  1. Spring Boot 配置文件详解

    2019独角兽企业重金招聘Python工程师标准>>> 第二篇 : Spring Boot配置文件详解 文章首发于微信公众号<程序员果果> 地址:https://mp.w ...

  2. spring boot配置文件详解

    spring boot配置文件详解 application.properties是spring-boot的核心配置文件,这个配置文件基本可以取代我们ssm或者ssh里面的所有的xml配置文件. 当我们 ...

  3. Spring Boot 单元测试详解+实战教程

    转载自   Spring Boot 单元测试详解+实战教程 Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring ...

  4. 《深入理解 Spring Cloud 与微服务构建》第十六章 Spring Boot Security 详解

    <深入理解 Spring Cloud 与微服务构建>第十六章 Spring Boot Security 详解 文章目录 <深入理解 Spring Cloud 与微服务构建>第十 ...

  5. 全面的Spring Boot配置文件详解

    全面的Spring Boot配置文件详解 Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门 ...

  6. SpringMVC基础--spring MVC配置详解

    牧涛 --<-<-<@态度决定一切→_→... 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔- 171  文章- 3  评论- 79  spring MVC配置详解 现在主流的 ...

  7. 一个SpringBoot问题就干趴下了?我却凭着这份PDF文档吊打面试官(Spring Boot知识点+详解)

    随着 Spring Boot 使用越来越广泛,Spring Boot 已经成为 Java 程序员面试的知识点,很多同学对 Spring Boot 理解不是那么深刻,经常就会被几个连环追问就给干趴下了! ...

  8. (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  9. SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪Spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

最新文章

  1. Jetty服务器jmx监控
  2. 《计算机科学概论》—第2章2.2节位置记数法
  3. bios设置 联想m8000t_联想怎样设置双显卡模式 联想设置双显卡模式方法【详解】...
  4. amd显卡风扇调节_非公版才是真爱 讯景XFX RX6800 XT海外版显卡评测
  5. Alpha阶段发布说明
  6. hive报错:hive create table: Specified key was too long; max key length is 767 bytes
  7. Python --之练习题
  8. DMA(2) S3C2410 DMA详解(其它的其实类似)
  9. 【译】使用 CocoaPods 模块化iOS应用
  10. centOS6.4下Percona-XtraBackup的安装
  11. linux系统函数 utime,utime函数
  12. history指令显示日期时间
  13. 多线程的Lock锁——ReentrantReadWriteLock
  14. 项目启动时就执行某些操作、@Scheduled定时项目启动时执行一次
  15. Cross Domian iFrame Exceptions 跨域iFrame屏蔽例外
  16. Android自拍相机应用——图片的镜像翻转
  17. 深度学习第四次培训(SVM算法)
  18. 学财会的懂计算机会加分吗,财会大学生在大学期间可以考这些证书
  19. Matlab GUI编程技巧(一):如何使gui编的界面一运行就居中
  20. AD/DA转换器性能限度

热门文章

  1. Adafruit_GFX matrix ws2812像素屏库使用教程AWTRIX2.0像素时钟
  2. [PaddleSeg源码阅读] PaddleSeg 导出静态图 export.py 文件中的道道
  3. IDEA怎么在单独的窗口显示差异和并排两排显示代码?
  4. 2007年金猪年各生肖运势
  5. ✨HeyUI新组件:TextEllipsis多行省略✨✨
  6. matlab数学实验教程实验1实验报告,Matlab数学实验一2015(答案版)
  7. java asm jndi_JNDI-Injection-Exploit JNDI注入利用工具
  8. 转载的JPeg压缩文档(很好,易懂)
  9. ListView聊天窗口与输入法键盘冲突解决方法(聊天框在viewpager里)
  10. TagSupport