SpringBoot專案中獲取配置檔案的配置資訊

2021-10-12 10:39:39 字數 1071 閱讀 2126

假如我們配置檔案.yaml的資訊是

myconfig:

username: abc

password: 123

或者.properties

myconfig.username=abc

myconfig.password=123

類需要被spring掃瞄到

@component

public class myconfig

@value()可以設定預設值

@value("myconfig.username:admin") //當沒有配置 myconfig.username的時候預設值是 admin

@value("myconfig.username:") //預設值是空字串

@component

public class myconfig

public string getpassword()

}

environment也可以設定預設值

environment.getproperty("myconfig.username",「admin」)//沒有配置myconfig.username的話預設值是admin
先在主類加上啟動配置註解 @enableconfigurationproperties

@enableconfigurationproperties

public static void main(string args) }

編寫獲取配置資訊的類

@component

@configurationproperties(prefix = "myconfig")

public class myconfig

@component 表示將該類標識為bean

@configurationproperties(prefix = "myconfig")用於繫結屬性,其中prefix表示所繫結的屬性的字首。

Springboot專案中Swagger的使用

2.7.0swagger.version properties io.springfoxgroupid springfox swagger uiartifactid version dependency io.springfoxgroupid springfox swagger2artifactid...

SpringBoot專案中Redis配置多資料庫

背景 乙個小需求,單redis服務例項下根據業務配置多個資料庫,需要自定義redis配置稍作改動才能實現,此時就不能redistemplate一步就位了 注 redis使用單執行緒 多路復用 io模型,別把多業務的併發全加在乙個redis例項上,最好多例項或者集群處理!基本的配置我就不多說了,可以參...

Spring Boot 專案中引入 flyway

org.flywaydb flyway core 5.2.4 flyway spring.flyway.baseline on migrate true spring.flyway.baseline description flyway baseline 禁止刪除 schema 下的 table s...