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è)試
編寫代碼獲取容器中的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)文章
淺談Spring Boot 2.0遷移指南主要注意點(diǎn)
Spring官方的Spring Boot 2變動(dòng)指南,主要是幫助您將應(yīng)用程序遷移到Spring Boot 2.0,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10SpringBoot特點(diǎn)之依賴管理和自動(dòng)裝配(實(shí)例代碼)
在使用SpringBoot的時(shí)候,會(huì)自動(dòng)將Bean裝配到IoC容器中,操作也很簡(jiǎn)單,今天小編給大家介紹下SpringBoot特點(diǎn)之依賴管理和自動(dòng)裝配的知識(shí),感興趣的朋友一起看看吧2022-03-03Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能的案例詳解
驗(yàn)證碼的作用可以有效防止其他人對(duì)某一個(gè)特定的注冊(cè)用戶用特定的程序暴力破解方式進(jìn)行不斷的登錄嘗試,接下來(lái)通過(guò)本文給大家介紹Spring?Boot實(shí)現(xiàn)登錄驗(yàn)證碼功能,需要的朋友可以參考下2022-08-08Java你不了解的大數(shù)型BigInteger與BigDecimal類
這篇文章主要介紹了Java 處理超大數(shù)類型之BigInteger與BigDecimal案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2022-05-05Java實(shí)現(xiàn)添加文字水印和圖片水印功能
為圖片添加水印是一種常用的圖片處理技術(shù),本文主要介紹了Java實(shí)現(xiàn)添加文字水印和圖片水印功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Spring Boot如何開(kāi)啟并使用郵件服務(wù)
這篇文章主要介紹了Spring Boot如何開(kāi)啟并使用郵件服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06