springboot 简单配置
1.文件格式
在 spring boot 中,有两种配置文件,一种是application.properties,另一种是application.yml,两种都可以配置spring boot 项目中的一些变量的定义,参数的设置等。下面来说说两者的区别。
application.properties 配置文件在写的时候要写完整,如:
spring.profiles.active=dev spring.datasource.data-username=root spring.datasource.data-password=root
在yml 文件中配置的话,写法如下:
spring: profiles: active: prod datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test username: root password: 123456
yml 文件在写的时候层次感强,而且少写了代码。所以现在很多人都使用yml配置文件。
2.多环境配置
在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如下:
spring: profiles: active: prod
这行配置在application.yml 文件中,意思是当前起作用的配置文件是application_prod.yml,其他的配置文件命名为 application_dev.yml,application_bat.yml等。
3.设置日志输出
# logging.file 日志输出路径 # logging.level 日志打印级别 logging: file: /.../Desktop/logs/info.log level: root: info org.springframework.web: debug
4.启动参数设置
server: prot: 8888 #配置访问的端口 servlet: context-path: /xxx #配置访问时的项目名
5.整合Mybatis
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/test driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.example.entity
6.整合redis
项目在使用redis时需要先添加依赖然后在配置文件(.yml)中加入一下配置文件,根据自己项目做相应调整
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
spring:
redis:
host: 127.0.0.1
port: 6379
password: 123456
pool:
max-active: 100
max-idle: 10
max-wait: 100000
timeout: 0