SpringBoot虽然简化配置,但也需要配置,配置文件的两种:application.properties和application.yml

默认创建spring-boot项目后,会在resources目录下生成一个空的application.properties

配置文件,springboot启动时加载该配置文件,application.properties中包含系统属性,环境变量,命令参数这类信息。

自定义配置:

       这些配置不一定要写在application.properties里面,可以在application.properties里面配置指定自定义文件名称和位置(无论配置写在哪,springboot都会读取加载application.properties文件)

spring.config.name=自定义的配置文件名称
spring.config.location=配置文件位置(可以是classpath或者有效的url)

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

多环境配置:

开发/测试/生产环境配置

spring.profiles.active=xxxx //该系统变量指明要使用的配置文件

一般应用于多环境配置分离,如生产环境(production),开发环境(development),测试环境呢(test)等可以自定义:如开发环境配置文件application-dev.properties则:spring.profiles.active=dev,在启动时会加载application-dev.properties

spring.profiles.active=prd

springboot java 指定配置文件目录 springboot自定义配置文件路径_配置文件

端口号配置:

server.address=xxx.xxx.xx.xxx //服务器绑定IP地址,多网卡时可以使用
server.port=8080              //指定springboot内嵌容器启动的端口
server.servlet.context-path=/greentranboot   //指定服务根目录

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

自定义启动图标

项目启动时会打印由符号组成的图标,是spring这几个字母,springboot里叫banner配置

#自定义输出信息的位置
banner.location=xxx.txt 
#指定编码格式
banner.charset=utf-8
#banner图开启或者打印模式
spring.main.banner-mode=console/off

默认图案:

springboot java 指定配置文件目录 springboot自定义配置文件路径_SpringBoot 配置_02

 在线生成图案的网站:http://patorjk.com/software/taag/

在/src/main/resources/新建一个banner.txt文件

将生成的图案复制到banner.txt文件中

springboot java 指定配置文件目录 springboot自定义配置文件路径_配置文件_03

视图解析:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

数据源配置:

.MySQL数据源配置(引入spring-boot-starter-jdbc自动集成)

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

.Oracle数据源配置

spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@172.117.145.113:1521:orcl
spring.datasource.username=root
spring.datasource.password=root

Redis配置(引入spring-boot-starter-data-redis自动集成)

spring.redis.host=localhost
spring.redis.port=6379
#连接池配置,默认配置
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=100ms
#密码,无密码不需要
spring.redis.password=
spring.redis.ssl=true /false

.MyBatis配置(引入mybatis-spring-boot-starter自动集成)

mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-location=classpath:mybatis/mappings/*.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