SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)
讀取application文件
在application.yml或者properties文件中添加:
user.address=china user.company=demo user.name=讓我康康
1、使用@Value注解讀取
直接 代碼如下:
package im.homeapi.controller; import org.springframework.beans.factory.annotation.Value; import org.omg.CORBA.PUBLIC_MEMBER; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @RestController @RequestMapping(value="/api") public class HomeController { @Value("${user.address}") private String address; @Value("${user.company}") private String company; @Value("${user.name}") private String name; //value 指定訪問地址,method 指定請求類型 @RequestMapping(value = "/home",method = RequestMethod.GET) public String Home() { return "Hello Word"; } @RequestMapping(value = "/getConfig") public String getConfig() { return "獲取的配置信息 :" + " name=" + name + " address=" + address + " , company=" + company; } }
放到單獨的配置類中讀?。?/p>
package im.homeapi.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class UserConfig { @Value("${user.address}") private String address; @Value("${user.company}") private String company; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Value("${user.name}") private String name; }
調(diào)用如下:
@Autowired private UserConfig userConfig; //讀取配置類 @RequestMapping(value = "/getConfigEntity") public String getConfigEntity() { return "獲取的配置信息 :" + " name=" + userConfig.getName() + " address=" + userConfig.getAddress() + " , company=" + userConfig.getCompany(); }
運行結(jié)果如下:
2、使用@ConfigurationProperties注解讀取方式
代碼如下:
package im.homeapi.entity; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "user") public class UserConfig1 { private String address; private String company; private String name; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
調(diào)用:
package im.homeapi.controller; import im.homeapi.entity.UserConfig; import im.homeapi.entity.UserConfig1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.omg.CORBA.PUBLIC_MEMBER; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @RestController @RequestMapping(value="/api") public class ConfigController { @Autowired private UserConfig1 userConfig; //讀取配置類 ConfigurationProperties注解讀取方式 @RequestMapping(value = "/getConfigEntity1") public String getConfigEntity() { return "獲取的配置信息 :" + " name=" + userConfig.getName() + " address=" + userConfig.getAddress() + " , company=" + userConfig.getCompany(); } }
運行結(jié)果:
3、讀取指定文件
3.1、@PropertySource+@Value注解讀取方式
在resources下新建配置config/db-config.properties
注意:@PropertySource不支持yml文件讀取。
db.username=root db.password=123456
如圖:
代碼:
package im.homeapi.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource(value = { "config/db-config.properties" }) public class DBConfig { @Value("${db.username}") private String username; @Value("${db.password}") private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
調(diào)用代碼:
package im.homeapi.controller; import im.homeapi.entity.DBConfig; import im.homeapi.entity.UserConfig; import im.homeapi.entity.UserConfig1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.omg.CORBA.PUBLIC_MEMBER; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @RestController @RequestMapping(value="/api") public class DbController { @Autowired private DBConfig dbConfig; //讀取配置類 PropertySource+@Value注解讀取方式 @RequestMapping(value = "/getConfigdb") public String getConfigdb() { return "獲取的配置信息 :" + " name=" + dbConfig.getUsername() + " , password=" + dbConfig.getPassword(); } }
運行結(jié)果:
3.2、@PropertySource+@ConfigurationProperties注解讀取方式
代碼:
package im.homeapi.entity; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "db") @PropertySource(value = { "config/db-config.properties" }) public class DBconfig1 { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
調(diào)用代碼:
@Autowired private DBconfig1 dbConfig1; //讀取配置類 @PropertySource+@ConfigurationProperties注解讀取方式 @RequestMapping(value = "/getConfigdb1") public String getConfigdb1() { return "獲取的配置信息 :" + " name=" + dbConfig1.getUsername() + " , password=" + dbConfig1.getPassword(); }
運行結(jié)果:
@Component 表示將該類標識為Bean
@ConfigurationProperties(prefix = "db")用于綁定屬性,其中prefix表示所綁定的屬性的前綴。
@PropertySource(value = "config/db-config.properties")表示配置文件路徑。
4、使用Environment讀取
代碼:
@Autowired private Environment environment; //讀取配置類 CEnvironment讀取方式 @RequestMapping(value = "/getConfigenv") public String getConfigenv() { return "獲取的配置信息 :" + " name=" + environment.getProperty("user.name") + " address=" + environment.getProperty("user.address") + " , company=" + environment.getProperty("user.company"); }
運行結(jié)果:
總結(jié)
從以上示例來看,Spring Boot可以通過@PropertySource,@Value,@Environment,@ConfigurationProperties來綁定變量。
到此這篇關(guān)于SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SpringBoot讀取配置數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring監(jiān)視器actuator配置應(yīng)用
這篇文章主要介紹了spring監(jiān)視器actuator配置應(yīng)用,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07java實現(xiàn)pdf文件截圖的方法【附PDFRenderer.jar下載】
這篇文章主要介紹了java實現(xiàn)pdf文件截圖的方法,結(jié)合實例形式分析了java基于PDFRenderer.jar進行pdf文件截圖的相關(guān)操作技巧,并附帶PDFRenderer.jar文件供讀者下載使用,需要的朋友可以參考下2018-01-01Java8函數(shù)式接口UnaryOperator用法示例
這篇文章主要介紹了Java8函數(shù)式接口UnaryOperator用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07SpringBoot使用prometheus監(jiān)控的示例代碼
這篇文章主要介紹了SpringBoot使用prometheus監(jiān)控的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決
這篇文章主要介紹了SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Java項目實戰(zhàn)之在線考試系統(tǒng)的實現(xiàn)(系統(tǒng)介紹)
這篇文章主要介紹了Java項目實戰(zhàn)之在線考試系統(tǒng)的實現(xiàn)(系統(tǒng)介紹),本文通過實例代碼,截圖的形式給大家展示系統(tǒng)技術(shù)架構(gòu),需要的朋友可以參考下2020-02-02