SpringBoot中application.yml基本配置詳情

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

  

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

  


其中context-path: /hotel能夠不用配置
若是配置,訪問路徑就是http://ip:port/hotel/
沒有配置,訪問路徑就是http://ip:port/html

(2)數據庫配置java

  

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

  

# 默認的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=prodredis

(3)指定靜態資源路徑spring

 

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中配置sql

<!--增長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中的值,也就是若是有用戶登錄的話,項目重啓後須要從新登錄。apache

<!--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:
            - /**
相關文章
相關標籤/搜索