Spring-Boot (二) application.properties配置文件内容

默认创建spring-boot项目后,会在resources目录下生成一个空的application.properties配置文件,springboot启动时加载该配置文件。

application.properties(或者application.yml)中包含系统属性、环境变量、命令参数这类信息。

下面简要说一部分spring-boot项目中application.properties内的一些常用配置,更多参照官方文档。

这些参数配置不一定要写在application.properties里面,可以在application.properties里面配置指定自定义配置文件名称和位置:(但是无论怎么配置,spring-boot都会读取加载application.properties文件)

spring.config.name=自定义的配置文件名称

spring.config.location=配置文件位置(可以是classpath或者有效url)

也可以通过在自定义类上设置@PropertySource注解指定读取某个配置文件

1. 命令行参数
server.address=xxx.xxx.xx.xxx  //服务器绑定ip地址,多网卡时可以指定

server.port=xxx 

//可以指定springboot内嵌容器启动的端口,默认使用tomcat容器时在8080端口,右键run- java application/springboot..,可以支持不同的容器,在引入不同的依赖时。当server.port=0时,表示自动扫面获取一个可用的端口。

ssl的安全访问配置


server.port=8443
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
server.ssl.key-password=another-secret
目前spring-boot不支持http和https同时启用的情况,只支持使用其中一个,如果需要同时使用,可以使用其他形式的实现方式。

该部分对应org.springframework.boot.autoconfigure.webServerProperties类。

此外还有一些不是很常用的如:

server.http2.enable=true/false

//该属性可以支持http2的协议类型,目前只支持tomcat和undertow的容器并且需要JDK1.8+.

官文上对于内嵌tomcat的配置参数也有很多,还可以编码来实现很多tomcat中的功能,不多说了,内容多,需要的看一下官网。

2. 系统属性/变量
开发/测试/生产环境配置
spring.profiles.active=xxxx

//该系统变量可以指明要使用的配置文件,一般应用于多环境配置分离,如生产环境(production),开发环境(development),测试环境(test)等,可以自定义,如开发环境配置文件为application-dev.properties,则spring.profiles.active=dev,在启动时会加载application-dev.properties配置文件。

关于Banner
项目启动时会打印一大坨符号组成的东西,看了好久感觉好像是打印的Spring这几个字母。

SpringBoot里叫banner配置。

banner.location=xxx.txt //可以自定义输出信息的位置

banner.charset=utf-8 //指定编码格式

spring.main.banner-mode=console/off    //banner图开启或者打印模式

Mysql数据源配置(引入spring-boot-starter-jdbc自动集成)
注解使用jdbcTemplate或者集成Mybatis(引入maven:mybatis-spring-boot-starter )

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/company?allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=xxxx

该部分配置为spring数据源的配置,使用jdbcTemplate进行数据库操作(使用jdbcTempate,会自动提示导入spring-boot-starter-jdbc等依赖)。在使用单元测试时,同时运行多个可能会出现数据源冲突,此时可以通过下面的属性生成唯一的数据源名称

spring.datasource.generate-unique-name=true

spring.datasource.*  的属性集配置对应org.springframework.boot.autoconfigure.jdbcDataSourceProperties类。

MongoDB数据源配置(引入spring-boot-starter-data-mongodb自动集成)
spring.data.mongodb.*对应的属性类为MongoProperties.java

常用基本属性:

spring.data.mongodb.host=xxx //默认localhost

spring.data.mongodb.port=xxx //默认27017

spring.data.mongodb.database=xx //数据库

spring.data.mongodb.username=xx //数据库用户名

spring.data.mongodb.password=xx //数据库用户密码

上述参数还可以用一个代替:(上述参数不可与该参数同时使用)

spring.data.mongodb.uri=mongodb://localhost:27017/test

如果有密码:

mongodb://username:password@localhost:27017/dbname

Redis配置(引入spring-boot-starter-data-redis自动集成)
#spring.redis.url=
#上面url形式等同下面
#host和port默认值取值localhost,6379
spring.redis.host=localhost
spring.redis.port=6379

#链接池配置,下面是默认值

spring.pool.max-idle=8

spring.pool.min-idel=0

spring.pool.max-wait=-1 //-1禁用该属性,等待单位milliseconds

spring.pool.max-active=8

#密码,无密码时不需要
#spring.redis.password=
#spring.redis.ssl=true/false

对应类org.springframework.boot.autoconfigure.data.redis.RedisProperties.java类

MyBatis配置(引入mybatis-spring-boot-starter自动集成)
如果不适用xml映射文件配置,下面的不需要,直接使用注解可以实现

#本例子中放在resources/mybatis/mybatis-config.xml中

mybatis.config-location=classpath:mybatis/mybatis-config.xml

mybatis.mapper-locaitons=classpath:mybatis/mappings/*.xml

#下面的配置可以在mybatis-config.xml中配置

#mybatis.configuration.*=xxx
#别名实体包,多个逗号隔开
#mybatis.type-aliases-package=com.tom.bean
#类型转换器包,多个逗号隔开
#mybatis.type-handlers-package=com.tom.mybatis.handlers
#执行类型
#mybatis.executor-type=SIMPLE/REUSE/BATCH

cache配置(引入spring-boot-starter-cache自动集成)
对于缓存自动集成,spring-boot也提供了很多方案,这里说一下默认的spring-cache、ehcache、redis-cache的相关属性配置

spring.cache.type=cache/ehcache/redis....(参照CacheType类) //用于指明要使用的缓存类型

ehcache专用:

spring.cache.ehcache.config=classpath:cache/ehcache.xml //用于配置使用ehcache时配置文件所在位置

在使用默认的cache时(默认cache是采用concurrentMap实现的缓存,前提是没有引入spring-boot-starter-data-redis等其他缓存依赖)或者使用redis时,可以使用下面参数配置

spring.cache.cache-names=xxx,xxx,xxx   //配置缓存名称

redis专用:

spring.cache.redis.time-to-live=600000    //缓存有效时间,单位毫秒

spring.cache.*对应类为:org.springframework.boot.autoconfigure.cache.cacheProperties.java

完成上面的设置就可以在项目中使用spring注解,但是要使用@EnableCaching启用注解,具体cache使用详见下面的章节。

文件上传配置
#默认true

#spring.http.mutipart.enabled=true

#上传中转文件位置,
#spring.http.multipart.location=
#最大上传文件大小,默认1MB
spring.http.multipart.max-file-size=5MB
#最大请求大小,默认10MB
spring.http.multipart.max-request-size=20MB

对应类:org.springframework.boot.autoconfigure.web.MultipartProperties

原文地址:https://blog.csdn.net/fly_leopard/article/details/78498804

猜你喜欢

转载自blog.csdn.net/qq_38085240/article/details/83141599