SpringBoot的配置文件

/ Java / 没有评论 / 1824浏览

application.properties基本使用

application.properties的使用,主要用来配置数据库连接、日志相关配置等。

应用配置文件位置

  • spring会从classpath下的/config目录或者classpath的根目录查找application.propertiesapplication.yml
  • /config优先于classpath根目录
  • @PropertySource这个注解可以指定具体的属性配置文件,优先级比较低。
  • 相同优先级位置同时有application.propertiesapplication.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

应用配置文件(.properties或.yml)

在配置文件中直接写:

name=MaxZhao
server.port=8080

.yml格式的配置文件如:

name: MaxZhao
server:
    port: 8080

当有前缀的情况下,使用.yml格式的配置文件更简单。详情请查看这里 注意:使用.yml时,属性名的值和冒号中间必须有空格,如name: MaxZhao正确,name:MaxZhao就是错的。

application.properties其它用法

自定义属性加载

通常我们需要配置文件来定义一些自己的属性,比如:

my.name=MaxZhao
my.sex=1

在代码中我们可以这样引用:

@Component
public class MyInfoProperties {
    @Value("${my.name}")
    private String name;
    @Value("${my.title}")
    private String sex;

    // 省略getter和setter
}
// 或者
@ConfigurationProperties(prefix="my")
public class MyInfoProperties { 
    private String name; 
    private String sex;

    // 省略getter和setter
}

然后其它类中引用

@Autowired
private MyInfoProperties myInfoProperties;

通过命令行设置属性值

java -jar xxx.jar --server.port=8888,这三通过使用—server.port属性来设置xxx.jar应用的端口为8888。 java -jar xxx.jar --server.port=8888命令,等价于我们在application.properties中添加属性server.port=8888

可以用SpringApplication.setAddCommandLineProperties(false)禁用命令行设置属性值。

通过代码设置属性值

SpringApplication application = new SpringApplication(Application.class);
Map<String, Object> defaultMap = new HashMap<String, Object>();
defaultMap.put("name", "MaxZhao");
//还可以是Properties对象
application.setDefaultProperties(defaultMap);
application.run(args); 

多环境配置

通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同。 在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。如:spring.profiles.active=test就会加载application-test.properties配置文件内容,可以用命令行执行java -jar xxx.jar --spring.profiles.active=test

按照上面的实验,可以如下总结多环境的配置思路:application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置。application-{profile}.properties中配置各个环境不同的内容。通过命令行方式去激活不同环境的配置。

随机数的使用

application.properties中:

# 随机字符串
random.value=${random.value}
# 随机int
random.number=${random.int}
# 随机long
random.bignumber=${random.long}
# 10以内的随机数
random.test1=${random.int(10)}
# 10-20的随机数
random.test2=${random.int[10,20]}

参数间的引用

application.properties中:

my.name=MaxZhao
my.sex=1
my.des=My name is ${MaxZhao}.

通过属性占位符还能缩短命令参数

例如修改web默认端口需要使用--server.port=9090方式,如果在配置中写上:

server.port=${port:8080}

那么就可以使用更短的--port=9090,当不提供该参数的时候使用默认值8080

Java系统属性

注意Java系统属性位置java -Dname="MaxZhao" -jar xxx.jar,可以配置的属性都是一样的,优先级不同。

例如java -Dname="MaxZhao" -jar xxx.jar --name="Spring!"name值为Spring!

属性名匹配规则,例如有如下配置对象:

@Component
@ConfigurationProperties(prefix="person")
public class ConnectionSettings {
    private String firstName;
} 

firstName可以使用的属性名如下:

  • person.firstName,标准的驼峰式命名
  • person.first-name,虚线(-)分割方式,推荐在.properties和.yml配置文件中使用
  • PERSON_FIRST_NAME,大写下划线形式,建议在系统环境变量中使用

devtools 配置

spring.devtools.restart.enabled=true
# 设置重启目录
spring.devtools.restart.additional-paths=src/main/java
# 排除不需要重启的目录,默认值在spring-boot-devtools/2.1.2.RELEASE/spring-boot-devtools-2.1.2.RELEASE-sources.jar!/org/springframework/boot/devtools/autoconfigure/DevToolsProperties.java
#spring.devtools.restart.exclude=

MySQL8 配置

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/maxzhao_ittest?charset=utf8mb4&useSSL=false
spring.datasource.username=maxzhao
spring.datasource.password=maxzhao
#com.mysql.cj.PerConnectionLRUFactory

Redis

# Redis 数据库索引
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=maxzhao
# 连接池最大连接数
spring.redis.jedis.pool.max-active=1000
# 连接池最大阻塞等待时间,负值没有限制
spring.redis.jedis.pool.max-wait=-1
# 连接池中最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
# 超时时间 毫秒
spring.redis.timeout=0