上篇咱們作了一些簡單的運行文件的配置,本篇帶領你們來認識經常使用的一些配置,固然了關於Spring Boot 這些配置太多太多了,若是想了解更多的話直接上官網參考一下,瞭解相關案例如本篇的配置。 application.properties配置官方指南參考。
html
在 IntelliJ IDEA 開發工具中建立項目的時候,默認的配置文件是application.properties,接下來咱們就學習一下而後配置一些咱們在開發中常常用到的配置項,進入帶領咱們揭開Spring Boot 項目的神祕的面紗。
在下面的文檔中我會在每一個配置中進行註解,這樣能更好的瞭解,固然官方文檔有咱們所需的所有配置,你們若是項目開發中有需求那麼能夠進入application.properties配置官方指南參考。mysql
英: Appendix A. Common application propertiesgit
1. Various properties can be specified inside your application.properties file, inside your application.yml file, or as command line switches. This appendix provides a list of common Spring Boot properties and references to the underlying classes that consume them. 2. [Note] Property contributions can come from additional jar files on your classpath, so you should not consider this an exhaustive list. Also, you can define your own properties. 3. [Warning] This sample file is meant as a guide only. Do not copy and paste the entire content into your application. Rather, pick only the properties that you need.
譯: 官方指南者三段話概況了今天咱們要講得內容:github
1. 能夠在application.properties文件中,application.yml文件中或命令行開關中指定各類屬性。 本附錄提供了經常使用Spring Boot屬性的列表以及對使用它們的基礎類的引用。 2. [注意] 屬性貢獻能夠來自類路徑上的其餘jar文件,所以您不該將此視爲詳盡的列表。 此外,您能夠定義本身的屬性。 3. [警告] 此示例文件僅供參考。 不要將整個內容複製並粘貼到您的應用程序中。 相反,只選擇您須要的屬性。
因此咱們在添加某些配置屬性的時候,必定要根據本身的須要來添加,否則有時出錯了,不知道哪裏找問題。spring
通常狀況下在src目錄下的/main/resource文件夾中新建application.properties
文件,目錄結構以下:sql
|--src |--main |--resources |--application.properties
咱們接下來編寫一些經常使用的屬性配置,你們在開發中須要到哪些就直接去查看一下:mongodb
#啓用調試日誌。 debug=false #啓用跟蹤日誌。 trace=false #-------------------------------------- # LOGGING 日記 #-------------------------------------- # 日誌配置文件的位置。 例如,Logback的classpath:logback.xml logging.config=classpath:logback.xml # 日誌文件名(例如,`myapp.log`)。名稱能夠是精確位置或相對於當前目錄。 logging.file=property.log # 最大日誌文件大小。 僅支持默認的logback設置 logging.file.max-size=10MB # 日誌文件的位置。 例如,`/ var / log`。 logging.path=/var/log #--------------------------------- # AOP #--------------------------------- # 使用AOP 切面編程 spring.aop.auto=true #是否要建立基於子類的(CGLIB)代理(true),而不是基於標準Java接口的代理(false) spring.aop.proxy-target-class=true #-------------------------------- # Email #-------------------------------- # 編碼格式 spring.mail.default-encoding=UTF-8 # SMTP服務器主機 spring.mail.host=smtp.property.com #SMTP服務器端口 spring.mail.port=7800 # 登陸SMTP用戶名 spring.mail.username=property # 登陸SMTP密碼 spring.mail.password=123456 #-------------------------------- # WEB 屬性配置 #-------------------------------- # 服務器應綁定的網絡地址 server.address=127.0.0.1 # 是否啓用了響應壓縮 server.compression.enabled=false # 鏈接器在關閉鏈接以前等待另外一個HTTP請求的時間。 未設置時,將使用鏈接器的特定於容器的默認值。 使用值-1表示沒有(即無限)超時 server.connection-timeout=2000 # 錯誤控制器的路徑 server.error.path=/error # 是否啓用HTTP / 2支持,若是當前環境支持它。 server.http2.enabled=false # 服務器端口默認爲:8080 server.port=8084 # SP servlet的類名。 server.servlet.jsp.class-name=org.apache.jasper.servlet.JspServlet # 主調度程序servlet的路徑。 server.servlet.path=/home # 會話cookie名稱 server.servlet.session.cookie.name=propertydemo #------------------------------ # HTTP encoding #------------------------------ # HTTP請求和響應的字符集。 若是未明確設置,則添加到「Content-Type」標頭。 spring.http.encoding.charset=UTF-8 # 是否啓用http編碼支持。 spring.http.encoding.enabled=true #-------------------- # MULTIPART (MultipartProperties) #-------------------- # 是否啓用分段上傳支持 spring.servlet.multipart.enabled=true # 上傳文件的中間位置 spring.servlet.multipart.location=/log # 最大文件的大小 spring.servlet.multipart.max-file-size=1MB # 最大請求大小 spring.servlet.multipart.max-request-size=10MB # 是否在文件或參數訪問時懶惰地解析多部分請求。 spring.servlet.multipart.resolve-lazily=false #-------------------------------------------- # SPRING SESSION JDBC (JdbcSessionProperties) #-------------------------------------------- # cron表達式用於過時的會話清理做業 spring.session.jdbc.cleanup-cron=0 * * * * * # 數據庫模式初始化模式 spring.session.jdbc.initialize-schema=embedded # 用於初始化數據庫模式的SQL文件的路徑 spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # 用於存儲會話的數據庫表的名稱 spring.session.jdbc.table-name=SPRING_SESSION #---------------------------------- # MONGODB 數據庫配置 #---------------------------------- # 數據庫名稱 spring.data.mongodb.database=demo # host 配置 spring.data.mongodb.host=127.0.0.1 # 登陸用戶名 spring.data.mongodb.username=property # 登陸密碼 spring.data.mongodb.password=123456 # 端口號,本身根據安裝的mongodb端口配置 spring.data.mongodb.port=9008 # 要啓用的Mongo存儲庫的類型 spring.data.mongodb.repositories.type=auto # 鏈接數據uri spring.data.mongodb.uri=mongodb://localhost/test #--------------------------------------- # DATASOURCE 數據庫配置 #--------------------------------------- # MySql jdbc Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 鏈接數據庫 # demo表示的是你建立的數據庫; spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&requireSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC # 數據庫用戶名 spring.datasource.username=root # 數據庫密碼 spring.datasource.password=123456 #----------------------------------- # Jpa使用 #----------------------------------- # 目標數據庫進行操做,默認狀況下自動檢測。可使用「databasePlatform」屬性設置。 #spring.jpa.database= demo1 # 要操做的目標數據庫的名稱,默認狀況下自動檢測。 也可使用「Database」枚舉來設置。 #spring.jpa.database-platform=DEMO # DDL模式 通常有這幾種方式,Spring Boot會根據是否定爲您的數據庫是嵌入式的,爲您選擇一個默認值 # update: 更新架構時,使用; spring.jpa.hibernate.ddl-auto=update # 是否啓用SQL語句的日誌記錄 spring.jpa.show-sql=true #---------------------------------------- # TESTING PROPERTIES #---------------------------------------- # 要替換的現有DataSource的類型 spring.test.database.replace=any # MVC打印選項 spring.test.mockmvc.print=default # ---------------你們查看文檔進行配置,不一一列舉了---------------------- # 各個屬性註解在查看經常使用配置文件application.properties中 # FREEMARKER # DEVTOOLS配置 # SPRING HATEOAS # HTTP message conversion # GSON # JDBC # JEST (Elasticsearch HTTP client) (JestProperties) # CASSANDRA (CassandraProperties) # --------------------------等等----------------------------------
查看經常使用配置文件 application.properties
因爲有時爲了方便項目的開發維護,咱們可能須要到自定義配置屬性,接下來咱們也來搞一下自定義屬性配置。數據庫
在 application.properties自定義配置屬性:
1.application.properties添加:apache
#-------------------------------- # 自定義屬性 #-------------------------------- com.eirunye.defproname="root" com.eirunye.defpropass="123456"
2.在DefPropertyController.class引用編程
@RestController public class DefPropertyController { @Value("${com.eirunye.defproname}") private String defProName; @Value("${com.eirunye.defpropass}") private String defProPass; @RequestMapping(value = "/defproprety") public String defPropretyUser() { return "這個自定義屬性名爲: " + defProName + ", 密碼爲:" + defProPass; } }
在獲取自定義屬性時必定要嚴格按照配置文件來獲取而且Value裏面的字符串必定是$
+花括號{***}
,花括號裏面的***
表示爲:application.properties裏面自定義的字符串
,因此本例就是表示爲: @Value("${com.eirunye.defproname}")
。
若是application.properties
有自定義爲 test.ok="haha"
,一樣的獲取方式爲:@Value("${test.ok}")
3.測試這裏暫未使用測試代碼的方式,後面的文章會講到
http://localhost:8084/defproprety
經過Bean的形式獲取
假如咱們遇到這樣狀況,自定義屬性多,而後每一個都是經過@Value(${""})
方式的話可能會很容易出錯,那麼咱們能夠採用如下方式。
1.新建一個Properties.class
添加@ConfigurationProperties(prefix = "com.eirunye")
//表示的是經過自定義屬性查找,若是自定義是:test.ok=haha
,則該這樣表示:@ConfigurationProperties(prefix = "test")
@ConfigurationProperties(prefix = "com.eirunye")//添加該註解 public class Properties { private String defproname; private String defpropass; // get/set方法 public String getDefproname() { return defproname; } public void setDefproname(String defproname) { this.defproname = defproname; } public String getDefpropass() { return defpropass; } public void setDefpropass(String defpropass) { this.defpropass = defpropass; }}
2.在controller包下建立 DefBeanPropertyController.class
@RestController public class DefBeanPropertyController { //經過 Autowired註解來獲取到 Properties屬性,注:Autowired是按類型進行裝配,可獲取它所裝配類的屬性 @Autowired Properties properties; @RequestMapping(value = "/bean/defproperty") public String getDefBeanProperties() { return "這是經過Bean註解的方式獲取屬性: " + properties.getDefproname() + ",密碼爲: " + properties.getDefpropass(); }}
3.在項目的入口文件Application添加註解@EnableConfigurationProperties
最後加上包名否則可能找不到掃描文件如:@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class})
。
@SpringBootApplication @EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class})//添加註解bean的掃描文件 public class DefpropertysApplication { public static void main(String[] args) { SpringApplication.run(DefpropertysApplication.class, args); } }
4.測試
IntelliJ IDEA訪問
建立文件xxx.properties文件方式
咱們能夠本身建立一個自定義屬性的文件如本例def.properties,(注:通常都是以 .properties 文件結尾)
1.添加自定義def.properties
配置以下:
#-------------------------------- # 自定義屬性 #-------------------------------- # 用戶名 com.eirunye.defineuser="property" # 年齡 com.eirunye.defineage=20
2.建立 DefineProperties.class
@Configuration @ConfigurationProperties(prefix = "com.eirunye")//添加註解 ConfigurationProperties "com.eirunye"表示的是自定義屬性 @PropertySource("classpath:defines.properties")// 添加註解 PropertySource 該註解能根據路徑掃描到咱們的文件 public class DefineProperties { // 這裏能夠經過@Value("${}")方式添加,我已經屏蔽掉了,直接經過ConfigurationProperties註解的方式 // @Value("${com.eirunye.defineuser}") private String defineuser; // @Value("${com.eirunye.defineage}") private int defineage; // get/set方法 public String getDefineuser() { return defineuser; } public void setDefineuser(String defineuser) { this.defineuser = defineuser; } public int getDefineage() { return defineage; } public void setDefineage(int defineage) { this.defineage = defineage; }}
3.在DefinePropertiesController.class
引用
@RestController public class DefinePropertiesController { @Autowired DefineProperties defineProperties; @RequestMapping(value = "define/Properties") public String getDefinePropertiesData(){ return "新建文件自定義屬性姓名:"+defineProperties.getDefineuser()+",新建文件自定義屬性年齡:"+defineProperties.getDefineage(); } }
4.別忘了在Application裏面添加配置@EnableConfigurationProperties
,即:`@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class,com.eirunye.defpropertys.bean.DefineProperties.class})
`
5.測試
http://localhost:8084/define/Properties
下載application.properties案例demo
因爲application.properties配置有點繁瑣,簡潔是咱們很是喜歡的,那麼在Spring Boot程序裏面固然也是能夠用.yml文件來配置的,接下來讓咱們進入對.yml文件的一些相關配置吧,官方文檔Using YAML Instead of Properties。
首先在src目錄下的/main/resource文件夾中新建application.yml、application-dev.yml、application-prod.yml
三個文件,刪除application.properties
文件,目錄結構以下:
|--src |--main |--resources |--application.yml |--application-dev.yml |--application-prod.yml
相信不少人要罵街了,這什麼狀況不是說,yml配置很給力嗎?怎麼還有建立那麼多文件,這不是比上面的 application.properties
配置還畫蛇添足嗎?莫急接下來讓咱們來看看.yml的配置以後,我相信你確定在項目中會喜歡用它。
常見的配置屬性以下: 【注意: 這裏屬性之間的間隔必須按照要求而來,如:冒號後面要空格】application.yml
配置
spring: profiles: active: dev #引用 application-dev.yml文件,這裏咱們能夠改成 prod,表示引用application-prod.yml文件 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&requireSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC username: root password: 12346 jpa: hibernate: ddl-auto: update show-sql: true data: mongodb: host: 127.0.0.1 uri: mongodb://localhost/test username: root password: 123456 database: test test: database: replace: any mockmvc: print: default servlet: multipart: enabled: true
application-dev.yml
能夠當成正式服務器端口
server: port: 8084
application-prod.yml
能夠當成測試服務器端口
server: port: 8080
在application.yml配置
1.和上面的application.properties相似,可是須要注意的是格式問題
com: eirunye: ymlname: ymlroot ymlpass: yml123456
2.經過@Value("${com.eirunye.ymlname}")
獲取
@RestController public class YmlPropertiesController{ @Value("${com.eirunye.ymlname}") private String ymlname; @Value("${com.eirunye.ymlpass}") private String ymlpass; @RequestMapping(value = "yml/proprety") public String getYmlPropreties() { return "這個自定義屬性名爲: " + ymlname+ ", 密碼爲:" + ymlpass; } }
3.測試
訪問:
http://localhost:8084/yml/proprety
經過Bean方式獲取和application.properties方式同樣
1.建立YmlPropertyBean.class
@ConfigurationProperties(prefix = "com.eirunye")//添加該註解 public class YmlPropertyBean { private String ymlname; private String ymlpass; // get/set方法..... }
2.在 YmlPropertyBeanController.class
引用
@RestController public class YmlPropertyBeanController { @Autowired YmlPropertyBean propertyBean; @RequestMapping(value = "/bean/ymlproperty") public String getYmlPropertyData(){ return "這個bean自定義屬性名爲: " + propertyBean.getYmlname()+ ", 密碼爲:" + propertyBean.getYmlpass(); } }
3.測試
訪問
http://localhost:8084/bean/ymlproperty
下載.yml 案例demo
1.本篇主要講得配置文件,到此就結束了,在開發中這是咱們常常用到。
2.在本篇有些相關配置,本例的代碼實例還沒涉及到,接下來會繼續結合相關的配置案例繼續更新。
3.相信你們也有所掌握。
你們想學習更多關於Spring Boot系列能夠查看