SpringBoot中application.yml基本配置詳情


把原有的application.properties刪掉。然后 maven -X clean install,或者通過Maven Project雙擊clean和install
(1)端口服務配置

  

#端口,項目上下文根
server:
  port: 8080
  servlet:
    context-path: /hotel

  


其中context-path: /hotel可以不用配置
如果配置,訪問路徑就是http://ip:port/hotel/
沒有配置,訪問路徑就是http://ip:port/

(2)數據庫配置

  

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,實現在不同的環境(比如開發、測試和生產環境)使用不同的配置變量。

  

# 默認的profile為dev,其他環境通過指定啟動參數使用不同的profile,比如:
#   測試環境:java -jar my-spring-boot.jar --spring.profiles.active=test
#   生產環境:java -jar my-spring-boot.jar --spring.profiles.active=prod
spring:
  profiles:
    active: dev

---
# 開發環境配置
spring:
  profiles: dev
mysql:
  ipPort: localhost:3306
  
---
# 測試環境配置
spring:
  profiles: test
mysql:
  ipPort: ip:port
  
---
# 生產環境配置
spring:
  profiles: prod
mysql:
  ipPort: ip:port

 


使用方法:
通過指定啟動參數使用不同的profile
測試環境: java -jar my-spring-boot.jar --spring.profiles.active=test
生產環境: java -jar my-spring-boot.jar --spring.profiles.active=prod

(3)指定靜態資源路徑

 

spring:
  resources:
    #指定靜態資源路徑,默認為classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/}

 

 

(4)熱部署
在Springboot+Thymeleaf的開發過程中,默認情況下修改到任何代碼都需要重新啟動項目才能生效。使用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只有設置為true時才會熱啟動,即當修改了html、css、js等這些靜態資源后不用重啟項目直接刷新即可。
然后修改application.yml

#熱部署--靜態資源立即生效
spring:
  #熱部署--靜態資源立即生效
  thymeleaf:
    cache: false
    encoding: UTF-8
    mode: LEGACYHTML5
    prefix: classpath:/templates/
    suffix: .html
    check-template-location: true
  #熱部署生效
  devtools:
    restart:
      enabled: true

 

  

如果需要在修改java文件后都能自動更新,則需要將原先的maven構建修改。
配置了true后在修改java文件后也就支持了熱啟動,不過這種方式是屬於項目重啟(速度比較快的項目重啟),會清空session中的值,也就是如果有用戶登陸的話,項目重啟后需要重新登陸。

<!--maven構建-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

 

 

<!--maven構建-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--如果沒有該項配置,devtools不會起作用,即應用不會restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

 

(5)時間配置

spring:
    jackson:
        #指定日期格式,比如yyyy-MM-dd HH:mm:ss
        date-format: yyyy-MM-dd HH:mm:ss
        #指定日期格式化時區
        time-zone: GMT+8

 

 

(6)模板配置
springboot 中自帶的頁面渲染工具為thymeleaf 還有freemarker 這兩種模板引擎 。

<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

  

spring:
   freemarker:
    suffix: .html   #設定模板的后綴
    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.##'   #數字格式化,無小數點

 

(7)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:
            - /**

 

 

作者:姜豬豬
來源:CSDN
原文:https://blog.csdn.net/zhulan316917864/article/details/84976234
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM