Spring成員對(duì)象注入的三種方式詳解
當(dāng)一個(gè)類(lèi)運(yùn)行需要調(diào)用一個(gè)成員對(duì)象,成員對(duì)象也是被容器類(lèi)托管的類(lèi)對(duì)象時(shí),則可以用依賴(lài)注入創(chuàng)建成員對(duì)象。讓容器類(lèi)來(lái)幫你創(chuàng)建成員對(duì)象。
官網(wǎng)鏈接:Annotation-based Container Configuration
前置:
容器類(lèi)AppConfig
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan public class AppConfig { }
創(chuàng)建一個(gè)接口Hello
public interface Hello { void HelloWorld(); }
創(chuàng)建一個(gè)類(lèi)HelloImpl1實(shí)現(xiàn)接口Hello。并且被容器托管
import org.springframework.stereotype.Component; @Component public class HelloImpl1 implements Hello{ @Override public void HelloWorld() { System.out.println("HelloWorld1"); } }
一、@Autowired注解
在聲明成員變量上加上@Autowires
注解。讓容器來(lái)幫忙創(chuàng)建對(duì)象。該成員變量也必須被容器類(lèi)托管。
創(chuàng)建MyHello類(lèi),里面有Hello成員對(duì)象。如下所示:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyHello { @Autowired Hello h; void say(){ h.HelloWorld(); } }
如果不加@Autowired
運(yùn)行say()會(huì)報(bào)錯(cuò)。
進(jìn)行測(cè)試:
import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); MyHello mh = ac.getBean("myHello",MyHello.class); mh.say(); } }
運(yùn)行結(jié)果:
如果成員對(duì)象是接口,并且有多個(gè)實(shí)現(xiàn)類(lèi)。則需要使用@Qualifier
或者@Primary
注解。
在創(chuàng)建一個(gè)類(lèi)實(shí)現(xiàn)Hello接口。
import org.springframework.stereotype.Component; @Component public class HelloImol2 implements Hello{ @Override public void HelloWorld() { System.out.println("HelloWorld2"); } }
這時(shí),Hello接口有兩個(gè)實(shí)現(xiàn)類(lèi)。
再次運(yùn)行Test類(lèi),報(bào)錯(cuò)。因?yàn)檎{(diào)用類(lèi)有沖突。
解決方案有兩種。
@Qualifier
在@Autowired
下加入@Qualifier(value="id名")
。id名默認(rèn)是類(lèi)名且首字母小寫(xiě)。要指定是調(diào)用實(shí)現(xiàn)接口中的哪個(gè)類(lèi)。
如上述解決:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class MyHello { @Autowired @Qualifier(value="helloImpl1") //添加此注解 Hello h; void say(){ h.HelloWorld(); } }
@Primary
在想要用到的多個(gè)實(shí)現(xiàn)接口對(duì)象中的其中一個(gè)類(lèi),加上@Primary
注解
如: 我想通過(guò)Hello運(yùn)行HelloImpl1。則在HelloImpl加上@Primary
注解:
import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component @Primary public class HelloImpl1 implements Hello{ @Override public void HelloWorld() { System.out.println("HelloWorld1"); } }
Test類(lèi)運(yùn)行成功
二、@Resource注解
在成員對(duì)象上加入@Resource(name="id名")
id名為你想要調(diào)用這個(gè)接口中實(shí)現(xiàn)的哪個(gè)類(lèi)的類(lèi)名且首字母小寫(xiě)。
則上述的MyHello類(lèi)可寫(xiě)成:
import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component public class MyHello { @Resource(name="helloImpl1") Hello h; void say(){ h.HelloWorld(); } }
運(yùn)行Test類(lèi)
三、@Inject 和 @Named注解
使用這兩個(gè)注解需要導(dǎo)入坐標(biāo)。在pom.xml加入
<dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
兩個(gè)注解一起用在需要?jiǎng)?chuàng)建成員對(duì)象上。其中@Named("id名")
id名為你想要調(diào)用這個(gè)接口中實(shí)現(xiàn)的哪個(gè)類(lèi)的類(lèi)名且首字母小寫(xiě)。
則上述的MyHello類(lèi)可以修改為:
import org.springframework.stereotype.Component; import javax.inject.Inject; import javax.inject.Named; @Component public class MyHello { @Inject @Named("helloImpl1") Hello h; void say(){ h.HelloWorld(); } }
繼續(xù)運(yùn)行Test類(lèi),仍然可以運(yùn)行成功
上述也可以實(shí)現(xiàn)set方法的依賴(lài)注入,需要保證傳入的參數(shù)被容器托管。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java springboot探究配置文件優(yōu)先級(jí)
在springboot項(xiàng)目中,我們可以通過(guò)在yml文件中設(shè)置變量,再通過(guò)@Value注解來(lái)獲得這個(gè)變量并使用,但如果這個(gè)項(xiàng)目已經(jīng)部署到服務(wù)器上,我們想更改這個(gè)數(shù)據(jù)了需要怎么做呢,其實(shí)在springboot項(xiàng)目中,配置文件是有優(yōu)先級(jí)的2023-04-04Java判斷數(shù)字位數(shù)的方法總結(jié)
本文給大家整理了Java判斷數(shù)字位數(shù)的兩種常用方法,對(duì)此有興趣的可以跟著小編一起學(xué)習(xí)下。2018-02-02解決idea 拉取代碼出現(xiàn)的 “ Сannot Run Git Cannot identify version of
這篇文章主要介紹了解決idea 拉取代碼出現(xiàn)的 “ Сannot Run Git Cannot identify version of git executable: no response“的問(wèn)題,需要的朋友可以參考下2020-08-08SpringBoot異步任務(wù)實(shí)現(xiàn)下單校驗(yàn)庫(kù)存的項(xiàng)目實(shí)踐
在開(kāi)發(fā)中,異步任務(wù)應(yīng)用的場(chǎng)景非常的廣泛,本文主要介紹了SpringBoot異步任務(wù)實(shí)現(xiàn)下單校驗(yàn)庫(kù)存的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09SpringBoot登錄判斷過(guò)程代碼實(shí)例
這篇文章主要介紹了SpringBoot登錄判斷代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12在VSCode里使用Jupyter?Notebook調(diào)試Java代碼的詳細(xì)過(guò)程
Jupyter Notebook是以網(wǎng)頁(yè)的形式打開(kāi),可以在網(wǎng)頁(yè)頁(yè)面中直接編寫(xiě)代碼和運(yùn)行代碼,代碼的運(yùn)行結(jié)果也會(huì)直接在代碼塊下顯示的程序,這篇文章主要介紹了在VSCode里使用Jupyter?Notebook,調(diào)試Java代碼,需要的朋友可以參考下2022-07-07解決SecureRandom.getInstanceStrong()引發(fā)的線程阻塞問(wèn)題
這篇文章主要介紹了解決SecureRandom.getInstanceStrong()引發(fā)的線程阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12