Spring Boot 之application.properties設定檔內容__Spring

來源:互聯網
上載者:User

Spring-Boot官方開發指導文檔

預設建立spring-boot項目後,會在resources目錄下產生一個空的application.properties設定檔,springboot啟動時載入該設定檔。

application.properties(或者application.yml)中包含系統屬性、環境變數、命令參數這類資訊。

下面簡要說一部分spring-boot項目中application.properties內的一些常用配置,更多參照官方文檔。

這些參數配置不一定要寫在application.properties裡面,可以在application.properties裡面配置指定自訂設定檔名稱和位置:(但是無論怎麼配置,spring-boot都會讀取載入application.properties檔案)

spring.config.name=自訂的設定檔名稱

spring.config.location=設定檔位置(可以是classpath或者有效url)

也可以通過在自訂類上設定@PropertySource註解指定讀取某個設定檔
1、命令列參數

server.address=xxx.xxx.xx.xxx  //伺服器綁定ip地址,多網卡時可以指定

server.port=xxx 

//可以指定springboot內嵌容器啟動的連接埠,預設使用tomcat容器時在8080連接埠,右鍵run- java application/springboot..,可以支援不同的容器,在引入不同的依賴時。當server.port=0時,表示自動掃面擷取一個可用的連接埠。

ssl的安全訪問配置

server.port=8443server.ssl.key-store=classpath:keystore.jksserver.ssl.key-store-password=secretserver.ssl.key-password=another-secret

目前spring-boot不支援http和https同時啟用的情況,只支援使用其中一個,如果需要同時使用,可以使用其他形式的實現方式。

該部分對應org.springframework.boot.autoconfigure.webServerProperties類。


此外還有一些不是很常用的如:

server.http2.enable=true/false

//該屬性可以支援http2的協議類型,目前只支援tomcat和undertow的容器並且需要JDK1.8+.


官文上對於內嵌tomcat的配置參數也有很多,還可以編碼來實現很多tomcat中的功能,不多說了,內容多,需要的看一下官網。


2、 系統屬性/變數 2.1 開發/測試/生產環境配置

spring.profiles.active=xxxx

//該系統變數可以指明要使用的設定檔,一般應用於多環境配置分離,如生產環境(production),開發環境(development),測試環境(test)等,可以自訂,如開發環境設定檔為application-dev.properties,則spring.profiles.active=dev,在啟動時會載入application-dev.properties設定檔。


2.2 關於Banner

項目啟動時會列印一大堆符號組成的東西,看了好久感覺好像是列印的Spring這幾個字母。

SpringBoot裡叫banner配置。

banner.location=xxx.txt //可以自訂輸出資訊的位置

banner.charset=utf-8 //指定編碼格式

spring.main.banner-mode=console/off    //banner圖開啟或者列印模式


2.3 Mysql資料來源配置(引入spring-boot-starter-jdbc自動整合)

註解使用jdbcTemplate或者整合Mybatis(引入maven:mybatis-spring-boot-starter )

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/company?allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=xxxx

該部分配置為spring資料來源的配置,使用jdbcTemplate進行資料庫操作(使用jdbcTempate,會自動提示匯入spring-boot-starter-jdbc等依賴)。在使用單元測試時,同時運行多個可能會出現資料來源衝突,此時可以通過下面的屬性產生唯一的資料來源名稱

spring.datasource.generate-unique-name=true

spring.datasource.*  的屬性集配置對應org.springframework.boot.autoconfigure.jdbcDataSourceProperties類。


2.4 MongoDB資料來源配置(引入spring-boot-starter-data-mongodb自動整合)

spring.data.mongodb.*對應的屬性類為MongoProperties.java

常用基本屬性:

spring.data.mongodb.host=xxx //預設localhost

spring.data.mongodb.port=xxx //預設27017

spring.data.mongodb.database=xx //資料庫

spring.data.mongodb.username=xx //資料庫使用者名稱

spring.data.mongodb.password=xx //資料庫使用者密碼

上述參數還可以用一個代替:(上述參數不可與該參數同時使用)

spring.data.mongodb.uri=mongodb://localhost:27017/test

如果有密碼:

mongodb://username:password@localhost:27017/dbname


2.5 Redis配置(引入spring-boot-starter-data-redis自動整合)

#spring.redis.url=
#上面url形式等同下面
#host和port預設值取值localhost,6379
spring.redis.host=localhost
spring.redis.port=6379

#連結池配置,下面是預設值

spring.pool.max-idle=8

spring.pool.min-idel=0

spring.pool.max-wait=-1 //-1禁用該屬性,等待單位milliseconds

spring.pool.max-active=8

#密碼,無密碼時不需要
#spring.redis.password=
#spring.redis.ssl=true/false

對應類org.springframework.boot.autoconfigure.data.redis.RedisProperties.java類


2.6 MyBatis配置(引入mybatis-spring-boot-starter自動整合)

如果不適用xml對應檔配置,下面的不需要,直接使用註解可以實現

#本例子中放在resources/mybatis/mybatis-config.xml中

mybatis.config-location=classpath:mybatis/mybatis-config.xml

mybatis.mapper-locaitons=classpath:mybatis/mappings/*.xml


#下面的配置可以在mybatis-config.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


2.7 cache配置(引入spring-boot-starter-cache自動整合)

對於緩衝自動整合,spring-boot也提供了很多方案,這裡說一下預設的spring-cache、ehcache、redis-cache的相關屬性配置

spring.cache.type=cache/ehcache/redis....(參照CacheType類) //用於指明要使用的緩衝類型

ehcache專用:

spring.cache.ehcache.config=classpath:cache/ehcache.xml //用於配置使用ehcache時設定檔所在位置


在使用預設的cache時(預設cache是採用concurrentMap實現的緩衝,前提是沒有引入spring-boot-starter-data-redis等其他緩衝依賴)或者使用redis時,可以使用下面參數配置

spring.cache.cache-names=xxx,xxx,xxx   //配置緩衝名稱


redis專用:

spring.cache.redis.time-to-live=600000    //緩衝有效時間,單位毫秒


spring.cache.*對應類為:org.springframework.boot.autoconfigure.cache.cacheProperties.java

完成上面的設定就可以在項目中使用spring註解,但是要使用@EnableCaching啟用註解,具體cache使用詳見下面的章節。


2.8 檔案上傳配置

#預設true

#spring.http.mutipart.enabled=true

#上傳中轉檔案位置,
#spring.http.multipart.location=
#最大上傳檔案大小,預設1MB
spring.http.multipart.max-file-size=5MB
#最大請求大小,預設10MB
spring.http.multipart.max-request-size=20MB

對應類:org.springframework.boot.autoconfigure.web.MultipartProperties


(注意:在實際類中出現駝峰表示的變數,配置屬性單詞間用-分割)


3、SpringBoot官網基本屬性集合 # ===================================================================   # COMMON SPRING BOOT PROPERTIES   #   # This sample file is provided as a guideline. Do NOT copy it in its   # entirety to your own application.               ^^^   # ===================================================================         # ----------------------------------------   # CORE PROPERTIES   # ----------------------------------------      # BANNER   banner.charset=UTF-8 # Banner file encoding.   banner.location=classpath:banner.txt # Banner file location.   banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).   banner.image.width= # Width of the banner image in chars (default 76)   banner.image.height= # Height of the banner image in chars (default based on image height)   banner.image.margin= # Left hand image margin in chars (default 2)   banner.image.invert= # If images should be inverted for dark terminal themes (default false)     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.