Spring中如何使用@Value注解實現(xiàn)給Bean屬性賦值
屬性賦值
只用Spring注解開發(fā)的時候,可以使用@Value搭配@PropertySource注解進行給Bean的屬性進行賦值。
@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ù)等位置上標(biāo)注。value屬性支持以下三種類型:
基本數(shù)據(jù)類型,包括String,int,boolean等類型。
SpEL,#{}的形式即表示使用SpEL表達(dá)式,一般用于獲取環(huán)境中其他bean的屬性,當(dāng)使用Elivis運算符“表達(dá)式1?:表達(dá)式2”,比如#{ “obj.property? :default_value” }形式時,這里的obj是取當(dāng)前容器中的對象,default_value就是前面的值為空時的默認(rèn)值。
在主配置類中通過@PropertySource注解加載配置文件,然后通過${ property : default_value}的形式取配置文件中的值,其中default_value表示前面的值為空時的默認(rèn)值。
測試
1.在添加了Spring依賴的Maven項目中創(chuàng)建
以下兩個實體類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對象的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)建一個配置文件person.properties
添加以下值:
student.name=張三 person.address=廣州市
3.創(chuàng)建配置類
使用@PropertySource注解讀取外部配置文件person.properties中的key/value保存到運行的環(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)建測試類進行測試
編寫代碼獲取容器中的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.測試結(jié)果:

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


以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot實現(xiàn)登錄驗證碼功能的案例詳解
驗證碼的作用可以有效防止其他人對某一個特定的注冊用戶用特定的程序暴力破解方式進行不斷的登錄嘗試,接下來通過本文給大家介紹Spring?Boot實現(xiàn)登錄驗證碼功能,需要的朋友可以參考下2022-08-08
Java你不了解的大數(shù)型BigInteger與BigDecimal類
這篇文章主要介紹了Java 處理超大數(shù)類型之BigInteger與BigDecimal案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2022-05-05

