這篇文章給大家分享的是有關SpringBoot屬性注入的方式有哪些的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
SpringBoot默認可以將application.properties文件或application.yml文件中定義的屬性值注入到java類中,這種注入實際上是通過java類屬性的setter方法進行的。
例:將application.yml中的以下屬性注入到類中:
## 自定義屬性
petshop:
name: 睿芽寵物
introduce: 種類齊全,安全可靠
licences: 1、上市許可證,2、疫苗許可證
infos: "{'phone':'36xx102','address':'xx省xx市'}"
使用@Value注解可以將application.yml中的屬性注入,@Value注解使用${屬性名}的方式來聲明要注入的屬性,如果要注入的屬性為Map集合,則需要結合Spel表達式進行處理。
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注解用于注入有著相同前綴的屬性,注入的方式也是通過java類的setter方法來完成,但是這種方式缺少了@Value注解的靈活性,也無法結合spel語言進行處理。
例:將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;
}測試注入的結果:
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;
}
}
使用@ConfigurationProperties注解可以將關聯的對象一同注入。
修改application.yml文件:
## 自定義屬性
petshop:
name: 睿芽寵物
introduce: 種類齊全,安全可靠
shopInfo:
phone: 36xx102
address: xx省xx市
licences: 上市許可證,疫苗許可證
pets:
- pet:
name: 金毛
price: 3365.21
- pet:
name: 巴哥
price: 2136.10
新建三個java類,并設置好引用關系:
@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;
}測試注入結果:
@RestController
@RequestMapping("/source")
public class SourceAction {
@Autowired
private PetShop petShop;
@RequestMapping("/show")
public Object show() {
return petShop;
}
}
在resource目錄下新建petshop/petshop.properties文件,將application.yml中的屬性轉換為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注解導入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發現可以得到與上例相同的結果。
感謝各位的閱讀!關于“SpringBoot屬性注入的方式有哪些”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。