Spring中如何使用@Value注解實(shí)現(xiàn)給Bean屬性賦值
屬性賦值
只用Spring注解開(kāi)發(fā)的時(shí)候,可以使用@Value搭配@PropertySource注解進(jìn)行給Bean的屬性進(jìn)行賦值。
@Value
@Value注解的定義:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
/**
* The actual value expression: for example {@code #{systemProperties.myProp}}.
*/
String value();
}
可以看到@Value注解接受的參數(shù)只能是字符串,所以使用時(shí)參數(shù)都需要帶上雙引號(hào)。并且可以在屬性,方法,參數(shù)等位置上標(biāo)注。value屬性支持以下三種類型:
基本數(shù)據(jù)類型,包括String,int,boolean等類型。
SpEL,#{}的形式即表示使用SpEL表達(dá)式,一般用于獲取環(huán)境中其他bean的屬性,當(dāng)使用Elivis運(yùn)算符“表達(dá)式1?:表達(dá)式2”,比如#{ “obj.property? :default_value” }形式時(shí),這里的obj是取當(dāng)前容器中的對(duì)象,default_value就是前面的值為空時(shí)的默認(rèn)值。
在主配置類中通過(guò)@PropertySource注解加載配置文件,然后通過(guò)${ property : default_value}的形式取配置文件中的值,其中default_value表示前面的值為空時(shí)的默認(rèn)值。
測(cè)試
1.在添加了Spring依賴的Maven項(xiàng)目中創(chuàng)建
以下兩個(gè)實(shí)體類Student類和Teacher類
public class Teacher {
public int id;
public String name;
public Teacher() {
// TODO Auto-generated constructor stub
System.out.println("teacher-----創(chuàng)建");
}
public Teacher(int id, String name) {
super();
this.id = id;
this.name = name;
}
//省略getter和setter方法
}
在Student類中使用${}取配置文件的值,#{}取容器中的teacher對(duì)象的name屬性
public class Student{
@Value("1")
private int id;
@Value("${student.name}")
private String name;
@Value("${student.address:北京}")
private String address;
@Value("#{teacher.name?:'老子'}")
private String teacherName;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", address=" + address + ", teacherName=" + teacherName + "]";
}
//省略getter和setter方法
}
2.在resources目錄下創(chuàng)建一個(gè)配置文件person.properties
添加以下值:
student.name=張三 person.address=廣州市
3.創(chuàng)建配置類
使用@PropertySource注解讀取外部配置文件person.properties中的key/value保存到運(yùn)行的環(huán)境變量中。
@Configuration
@PropertySource("person.properties")
public class SpringConfig2 {
@Bean
public Student student() {
return new Student();
}
@Bean
public Teacher teacher() {
return new Teacher(2,"老李");
}
}
4.創(chuàng)建測(cè)試類進(jìn)行測(cè)試
編寫(xiě)代碼獲取容器中的id為student的bean和直接獲取配置環(huán)境的屬性值。
public class IoCTest {
@Test
public void test() {
//獲取Spring的IOC容器
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfig2.class);
//從容器中獲取bean
Student stu = (Student) applicationContext.getBean("student");
System.out.println(stu);
//獲取環(huán)境變量
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("person.address");
System.out.println(property);
//關(guān)閉容器
applicationContext.close();
}
}
5.測(cè)試結(jié)果:

如何給Bean的屬性賦值(注入)
1.通過(guò)構(gòu)造方法設(shè)置值。
2.設(shè)置注入(通過(guò)set方法)
通過(guò)property標(biāo)簽(內(nèi)部是通過(guò)調(diào)用類的set方法將值注入)


以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Apache?log4j2-RCE?漏洞復(fù)現(xiàn)及修復(fù)建議(CVE-2021-44228)
Apache?Log4j2是一款Java日志框架,大量應(yīng)用于業(yè)務(wù)系統(tǒng)開(kāi)發(fā)。2021年11月24日,阿里云安全團(tuán)隊(duì)向Apache官方報(bào)告了Apache?Log4j2遠(yuǎn)程代碼執(zhí)行漏洞(CVE-2021-44228),本文給大家介紹Apache?log4j2-RCE?漏洞復(fù)現(xiàn)(CVE-2021-44228)的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-12-12
一篇文章告訴你如何在Java數(shù)組中插入一個(gè)字符
本篇文章主要介紹了Java數(shù)組中插入一個(gè)字符的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-10-10
Spring中基于Java的配置@Configuration和@Bean用法詳解
這篇文章主要介紹了Spring中基于Java的配置@Configuration和@Bean用法詳解,Spring中為了減少xml中配置,可以聲明一個(gè)配置類(例如SpringConfig)來(lái)對(duì)bean進(jìn)行配置。,需要的朋友可以參考下2019-06-06
Java代碼實(shí)現(xiàn)隨機(jī)生成漢字的方法
今天小編就為大家分享一篇關(guān)于Java代碼實(shí)現(xiàn)隨機(jī)生成漢字的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Springboot利用Redis實(shí)現(xiàn)接口冪等性攔截
這篇文章主要為大家介紹了Springboot如何利用Redis實(shí)現(xiàn)接口冪等性攔截。本文將通過(guò)自定義注解+redis+攔截器+MD5?實(shí)現(xiàn),感興趣的可以了解一下2022-06-06
java數(shù)據(jù)結(jié)構(gòu)與算法之簡(jiǎn)單選擇排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之簡(jiǎn)單選擇排序,結(jié)合實(shí)例形式分析了選擇排序的原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05

