淺談springboot 屬性定義
本文介紹了淺談springboot 屬性定義,分享給大家。具體如下:
簡單屬性自定義
一般屬性可以定義在通用的配置文件application.properties里面
# 自定義屬性 boot.userName = yuxi
如何獲取呢?
按照spring的獲取方式就可以了,很簡單
@Value(value = "${boot.userName}")
private String userName;
復(fù)雜屬性自定義
在配置里配置屬性
# 復(fù)雜屬性 test.id=1 test.name=xiaoyuxixi test.money=100000000
定義實(shí)體
//需要注意這個屬性是必須的
@ConfigurationProperties(prefix = "test")
public class Account {
private int id;
private String name;
private double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
注入屬性
@RestController
// 這個屬性也是必須的
@EnableConfigurationProperties({Account.class})
public class HelloController {
//自定義屬性
@Value(value = "${boot.userName}")
private String userName;
@Autowired
private Account account;
/**
* 復(fù)雜 屬性自定義
*
* @return
*/
@RequestMapping("/hard")
public Object getHardProperties() {
return account;
}
/**
* welcome spring boot
*
* @return
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "Greetings from Spring Boot! ";
}
/**
* 簡單 屬性自定義
*
* @return
*/
@RequestMapping("/user")
public String getProperties() {
System.out.println(userName);
return userName;
}
}
在配置完復(fù)雜的屬性之后,會發(fā)現(xiàn)這樣寫的話 application.properties里內(nèi)容會很多有很多屬性不是公共的配置,放在這里不是有優(yōu)雅,可以把這些配置單獨(dú)寫一個配置文件
配置文件獲取
添加配置文件 (test.properties)
# 配置文件獲取 lakala.id=1 lakala.name=xiaoyuxixi lakala.money=100000000
獲取屬性文件(在實(shí)體上加入以下配置文件)
@Configuration @PropertySource(value = "classpath:test.properties")
源碼地址:springbootlearning_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot在idea中的 .idea和 .iml文件的作用
本文主要介紹了SpringBoot在idea中的 .idea和 .iml文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
SpringBoot中@EnableAutoConfiguration注解源碼分析
這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration注解源碼分析,@EnableAutoConfiguration,主要是用于加載Starter目錄包之外的、需要Spring自動生成Bean對象的、帶有@Configuration注解的類,需要的朋友可以參考下2023-08-08
利用SpringMVC和Ajax實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了利用SpringMVC和Ajax實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
SpringMvc實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了SpringMvc實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
mybatis的insert語句插入數(shù)據(jù)時的返回值的實(shí)現(xiàn)
這篇文章主要介紹了mybatis的insert語句插入數(shù)據(jù)時的返回值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Springboot自定義mvc組件如何實(shí)現(xiàn)
這篇文章主要介紹了Springboot自定義mvc組件如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

