zl程序教程

您现在的位置是:首页 >  后端

当前栏目

spring boot配置文件详解

2023-09-14 09:02:02 时间

spring boot配置文件详解

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


当我们启动springboot工程做的第一件事就是加载application.properties属性配置文件。


application.properties虽然有时候是空的,也能正常启动,是因为,application.properties有很多默认配置,这也说明了springboot是一个开箱即用的。也就是约定优于配置的实践。

Springboot有很多默认配置参考springboot文档
比如:编码对中文支持很友好,统一utf-8

spring.messages.encoding=UTF-8 # Message bundles encoding.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type"

比如:tomcat端口号
server.port=8080 # Server HTTP port.

//pojo中有date类型怎么转string类型就靠这两个配置

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

spring.jackson.time-zone=Asia/Chongqing

参数间引用

com.niwotaxuexiba.author.name=zhangxueliang
com.niwotaxuexiba.author.sex= male
com.niwotaxuexiba.author.desc=${com.niwotaxuexiba.author.name}  ${com.niwotaxuexiba.author.sex}

随机数

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