(1)端口服務(wù)配置
server:
port: 8080 #端口號
servlet:
context-path: /main #項目訪問路徑
(2)數(shù)據(jù)庫配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
redis:
database: 0
host: localhost
port: 6379
password:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 0
(3)配置多個不同的profile,實現(xiàn)在不同的環(huán)境(比如開發(fā)、測試和生產(chǎn)環(huán)境)使用不同的配置變量。
# 默認的profile為dev,其他環(huán)境通過指定啟動參數(shù)使用不同的profile,比如:
# 測試環(huán)境:java -jar my-spring-boot.jar --spring.profiles.active=test
# 生產(chǎn)環(huán)境:java -jar my-spring-boot.jar --spring.profiles.active=prod
spring:
profiles:
active: dev
---
# 開發(fā)環(huán)境配置
spring:
profiles: dev
mysql:
ipPort: localhost:3306
---
# 測試環(huán)境配置
spring:
profiles: test
mysql:
ipPort: ip:port
---
# 生產(chǎn)環(huán)境配置
spring:
profiles: prod
mysql:
ipPort: ip:port
使用方法:
通過指定啟動參數(shù)使用不同的profile
測試環(huán)境: java -jar my-spring-boot.jar --spring.profiles.active=test
生產(chǎn)環(huán)境: java -jar my-spring-boot.jar --spring.profiles.active=prod
在配置文件中指定 spring.profiles.active=dev
虛擬機參數(shù)
?-Dspring.profiles.active=dev
springboot 啟動會掃描以下位置的application.properties或者application.yml文件作為Spring boot的默認配置文件
–file:./config/
–file:./
–classpath:/config/
–classpath:/
優(yōu)先級由高到底,高優(yōu)先級的配置會覆蓋低優(yōu)先級的配置
SpringBoot會從這四個位置全部加載主配置文件,互補配置;
項目打包好以后,我們可以使用命令行參數(shù)的形式,啟動項目的時候來指定配置文件的新位置;指定配置文件和默認加載的這些配置文件共同起作用形成互補配置;
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties
(4)指定靜態(tài)資源路徑
spring:
resources:
#指定靜態(tài)資源路徑,默認為classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/}
(5)熱部署
在Springboot+Thymeleaf的開發(fā)過程中,默認情況下修改到任何代碼都需要重新啟動項目才能生效。使用spring.thymeleaf.cache和devtools來解決html熱啟動的問題
首先在pom.xml中配置
<!--增加maven的devtools依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
注意:true只有設(shè)置為true時才會熱啟動,即當修改了html、css、js等這些靜態(tài)資源后不用重啟項目直接刷新即可。
然后修改application.yml
#熱部署--靜態(tài)資源立即生效
spring:
#熱部署--靜態(tài)資源立即生效
thymeleaf:
cache: false #是否開啟模板緩存,默認true
encoding: UTF-8 #指定模板的編碼,默認為: UTF-8
mode: HTML #指定模板的模式,具體查看StandardTemplateModeHandlers,默認為: HTML5
prefix: classpath:/templates/ #前綴
suffix: .html #后綴
check-template-location: true #是否檢查模板路徑是否存在,默認true
servlet:
content-type: text/html #響應(yīng)類型,指定Content-Type,默認為: text/html
#熱部署生效
devtools:
restart:
enabled: true
(6)時間配置
spring:
jackson:
#指定日期格式,比如yyyy-MM-dd HH:mm:ss
date-format: yyyy-MM-dd HH:mm:ss
#指定日期格式化時區(qū)
time-zone: GMT+8
(7)模板配置
springboot 中自帶的頁面渲染工具為thymeleaf 還有freemarker 這兩種模板引擎 。
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
spring:
freemarker:
suffix: .html #設(shè)定模板的后綴
request-context-attribute: request #request訪問request
content-type: text/html
enabled: true
cache: false #緩存配置
template-loader-path: classpath:/templates/ #模板加載路徑 按需配置
charset: UTF-8 #編碼格式
settings:
number_format: '0.##' #數(shù)字格式化,無小數(shù)點
(8)redis和shiro配置
<!--redis和shiro-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
spring:
redis:
database: 0
host: localhost
port: 6379
password:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 0
shiro:
conf:
domain:
cookiePath: /
successUrl: /index
loginView: /login
openToken: false
sessionTimeout: 1800000
algorithmName: md5
hashIterations: 5
#不攔截的路徑
sysanon:
- /login
- /regist
#跨域配置
allowedOrigins:
- /**