src/main/resources目录是SpringBoot的配置目录,SpringBoot使用一个全局的配置文件,默认配置文件位置为: src/main/resources/application.properties。其配置文件名是固定的。application.properties 或者是application.yml,例如修改服务器端口
server:port: 8081
@Value获取值和@ConfigurationProperties获取值比较
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量注入配置文件中的属性 | 一个个指定 | 
| 松散绑定(松散语法) | 支持 | 不支持 | 
| SpEL | 不支持 | 支持 | 
| JSR303数据校验 | 支持 | 不支持 | 
| 复杂类型封装 | 支持 | 不支持 | 
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
@PropertySource:加载指定的配置文件;
| 配置项 | 说明 | 举例 | 
|---|---|---|
| server.port | 应用程序启动端口 | server.port=8080,定义应用程序启动端口为 8080 | 
| server.servlet.context-path | 应用程序上下文 | server.servlet.context-path=/api,则访问地址为:http://ip:port/api | 
| spring.servlet.multipart.maxFileSize | 最大文件上传大小,-1为不限制 | spring.servlet.multipart.maxFileSize=-1 | 
| spring.jpa.database | 数据库类型 | spring.jpa.database=MYSQL,指定数据库为mysql | 
| spring.jpa.properties.hibernate.dialect | hql方言 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect | 
| spring.datasource.url | 数据库连接字符串 | spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true | 
| spring.datasource.username | 数据库用户名 | spring.datasource.username=root | 
| spring.datasource.password | 数据库密码 | spring.datasource.password=root | 
| spring.datasource.driverClassName | 数据库驱动 | spring.datasource.driverClassName=com.mysql.jdbc.Driver | 
| spring.jpa.showSql | 控制台是否打印 SQL 语句 | spring.jpa.showSql=true | 
例如application.yml 配置文件内容:
server:port: 8080servlet:context-path: /apitomcat:basedir: /data/tmpmax-threads: 1000min-spare-threads: 50connection-timeout: 5000spring:profiles:active: devservlet:multipart:maxFileSize: -1datasource:url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=trueusername: rootpassword: rootdriverClassName: com.mysql.jdbc.Driverjpa:database: MYSQLshowSql: truehibernate:namingStrategy: org.hibernate.cfg.ImprovedNamingStrategyproperties:hibernate:dialect: org.hibernate.dialect.MySQL5Dialectmybatis:configuration:#配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性map-underscore-to-camel-case: true
多环境配置
创建 application.yml 文件,在里面添加如下内容:
spring:profiles:active: dev
含义是指定当前项目的默认环境为 dev,即项目启动时如果不指定任何环境,Spring Boot 会自动从 dev 环境文件中读取配置信息。我们可以将不同环境都共同的配置信息写到这个文件中。
然后创建多环境配置文件,文件名的格式为:application-{profile}.yml,其中,{profile} 替换为环境名字,比如:
application-dev.properties:开发环境application-test.properties:测试环境application-prod.properties:生产环境
如 application-dev.yml,我们可以在其中添加当前环境的配置信息,如添加数据源:
spring:datasource:url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=trueusername: rootpassword: rootdriverClassName: com.mysql.jdbc.Driver
然后我们在application.properties中设置spring.profiles.active=dev,就是说默认以dev环境设置
在每次编译打包我们无需修改任何东西,编译为 jar 文件后,运行命令:
java -jar api.jar --spring.profiles.active=dev
其中 --spring.profiles.active 就是我们要指定的环境。
读取配置
1.读取application文件
在application.yml或者properties文件中添加:
info:name: xiaomingage: 13sex: 1
读取方式如下:
import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** 通过@value注解的方式读取*/@Componentpublic class TechUser {@Value("${info.name}")private String name;@Value("${info.age}")private int age;@Value("${info.sex}")private int sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getSex() {return sex;}public void setSex(int sex) {this.sex = sex;}}@ConfigurationProperties注解读取方式import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "info")public class TechUser {private String name;private int age;private int sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getSex() {return sex;}public void setSex(int sex) {this.sex = sex;}}
2.读取指定文件
资源目录下建立config/db-config.properties:
db.username=rootdb.password=123456
2.1@PropertySource+@Value注解读取方式
@Component@PropertySource(value = { "config/db-config.properties" })public class DBConfig1 {@Value("${db.username}")private String username;@Value("${db.password}")private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
2.2@PropertySource+@ConfigurationProperties注解读取方式
@Component@ConfigurationProperties(prefix = "db")@PropertySource(value = { "config/db-config.properties" })public class DBConfig2 {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
3.Environment读取方式
以上所有加载出来的配置都可以通过Environment注入获取到。
@Autowiredprivate Environment env;
在Spring Boot 2.0中对配置属性加载的时候会除了像1.x版本时候那样移除特殊字符外,还会将配置均以全小写的方式进行匹配和加载。所以,下面的4种配置方式都是等价的:
properties格式:
spring.jpa.databaseplatform=mysqlspring.jpa.database-platform=mysqlspring.jpa.databasePlatform=mysqlspring.JPA.database_platform=mysql
yaml格式:
spring:jpa:databaseplatform: mysqldatabase-platform: mysqldatabasePlatform: mysqldatabase_platform: mysql
Tips:推荐使用全小写配合-分隔符的方式来配置,比如:spring.jpa.database-platform=mysql
