SpringBoot屬性注入的多種方式實(shí)例
一、@Value注解注入屬性
SpringBoot默認(rèn)可以將application.properties文件或application.yml文件中定義的屬性值注入到j(luò)ava類中,這種注入實(shí)際上是通過java類屬性的setter方法進(jìn)行的。
例:將application.yml中的以下屬性注入到類中:
## 自定義屬性
petshop:
name: 睿芽寵物
introduce: 種類齊全,安全可靠
licences: 1、上市許可證,2、疫苗許可證
infos: "{'phone':'36xx102','address':'xx省xx市'}"
使用@Value注解可以將application.yml中的屬性注入,@Value注解使用${屬性名}的方式來聲明要注入的屬性,如果要注入的屬性為Map集合,則需要結(jié)合Spel表達(dá)式進(jìn)行處理。
package com.it.action;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/source")
public class SourceAction {
@Value("${petshop.name}")
private String name;
@Value("${petshop.introduce}")
private String introduce;
@Value("${petshop.licences}")
private List<String> licences;
@Value("#{${petshop.infos}}")
private Map<String, String> infos;
@RequestMapping("/show")
public Object show() {
Map<String, Object> map = new LinkedHashMap();
map.put("name", name);
map.put("introduce", introduce);
map.put("licences", licences);
map.put("infos", infos);
return map;
}
}
訪問http://localhost:8080/source/show觀察被注入的屬性:

二、@ConfigurationProperties注解批量注入屬性
@ConfigurationProperties注解用于注入有著相同前綴的屬性,注入的方式也是通過java類的setter方法來完成,但是這種方式缺少了@Value注解的靈活性,也無法結(jié)合spel語言進(jìn)行處理。
例:將application.yml中的以下屬性注入到類中:
## 自定義屬性
petshop:
name: 睿芽寵物
introduce: 種類齊全,安全可靠
licences: 上市許可證,疫苗許可證
infos:
- phone: 36xx102
- address: xx省xx市
新建PetShop類并注入屬性:
package com.it.vo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Data
@Component
@ConfigurationProperties(prefix = "petshop")
public class PetShop {
private String name;
private String introduce;
private List<String> licences;
private Map<String, String> infos;
}
測試注入的結(jié)果:
package com.it.action;
import com.it.vo.PetShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/source")
public class SourceAction {
@Autowired
private PetShop petShop;
@RequestMapping("/show")
public Object show() {
return petShop;
}
}

三、注入實(shí)體對象
使用@ConfigurationProperties注解可以將關(guān)聯(lián)的對象一同注入。
修改application.yml文件:
## 自定義屬性
petshop:
name: 睿芽寵物
introduce: 種類齊全,安全可靠
shopInfo:
phone: 36xx102
address: xx省xx市
licences: 上市許可證,疫苗許可證
pets:
- pet:
name: 金毛
price: 3365.21
- pet:
name: 巴哥
price: 2136.10
新建三個java類,并設(shè)置好引用關(guān)系:
@Data
public class PetShopInfo {
private String phone;
private String address;
private List<String> licences;
}
@Data
public class Pet {
private String name;
private double price;
}
@Data
@Component
@ConfigurationProperties(prefix = "petshop")
public class PetShop {
private String name;
private String introduce;
private PetShopInfo shopInfo;
private List<Pet> pets;
}
測試注入結(jié)果:
@RestController
@RequestMapping("/source")
public class SourceAction {
@Autowired
private PetShop petShop;
@RequestMapping("/show")
public Object show() {
return petShop;
}
}

四、自定義文件注入
在resource目錄下新建petshop/petshop.properties文件,將application.yml中的屬性轉(zhuǎn)換為properties中的key-value格式:
## 自定義屬性
petshop.name=睿芽寵物
petshop.introduce=種類齊全,安全可靠petshop.shopInfo.phone=36xx102
petshop.shopInfo.address=xx省xx市
petshop.shopInfo.licences=上市許可證,疫苗許可證petshop.pets[0].name=金毛
petshop.pets[0].price=3365.21petshop.pets[1].name=巴哥
petshop.pets[1].price=2136.10
修改PetShop類,添加@PropertySource注解導(dǎo)入properties文件
@Data
@Component
@PropertySource(value = "classpath:petshop/petshop.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "petshop")
public class PetShop {
private String name;
private String introduce;
private PetShopInfo shopInfo;
private List<Pet> pets;
}
訪問http://localhost:8080/source/show發(fā)現(xiàn)可以得到與上例相同的結(jié)果。
總結(jié)
到此這篇關(guān)于SpringBoot屬性注入的多種方式的文章就介紹到這了,更多相關(guān)SpringBoot屬性注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mall整合SpringSecurity及JWT實(shí)現(xiàn)認(rèn)證授權(quán)實(shí)戰(zhàn)
這篇文章主要為大家介紹了mall整合SpringSecurity及JWT實(shí)現(xiàn)認(rèn)證授權(quán)實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
SpringBoot?DataSource數(shù)據(jù)源實(shí)現(xiàn)自動配置流程詳解
這篇文章主要介紹了SpringBoot?DataSource數(shù)據(jù)源實(shí)現(xiàn)自動配置流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
Java中的SimpleDateFormat的線程安全問題詳解
這篇文章主要介紹了Java中的SimpleDateFormat的線程安全問題詳解,sonar 是一個代碼質(zhì)量管理工具,SonarQube是一個用于代碼質(zhì)量管理的開放平臺,為項(xiàng)目提供可視化報告, 連續(xù)追蹤項(xiàng)目質(zhì)量演化過程,需要的朋友可以參考下2024-01-01
解決RabbitMq消息隊(duì)列Qos?Prefetch消息堵塞問題
這篇文章主要為大家介紹了關(guān)于如何解決解決RabbitMq?Qos?Prefetch消息堵塞的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01

