聊聊@Autowired注解注入,寫接口名字還是實(shí)現(xiàn)類的名字
用@Autowired注解注入,寫接口名字還是實(shí)現(xiàn)類的名字
來自某程序員一個(gè)問答問題
1. 有一點(diǎn)沒明白,為什么注解@repository注解的是接口UserDAO的實(shí)現(xiàn)類UserDAOImpl,而在UserServiceImpl中使用@Autowired注解注入屬性private UserDAO userDAO自動(dòng)裝配,為什么最后得到的是UserDAOImpl的實(shí)例。
-----上面是某位同學(xué)的提問,我也有這樣的疑問----
2. @Service注解服務(wù)層的時(shí)候,在unitest中,是從ApplicationContext.getBean("實(shí)現(xiàn)類名字,首字母小寫") 這樣獲取的。
也就是說,在容器中初始化的Bean應(yīng)該按照實(shí)現(xiàn)類名字規(guī)則。 這一點(diǎn)如果是用xml配置是不存在這樣的問題,因?yàn)閤ml可以指定id, id 是接口,class指向?qū)崿F(xiàn)類。
3. 來自網(wǎng)友的回答:這個(gè)其實(shí)是創(chuàng)建了實(shí)現(xiàn)類的對(duì)象但引用了接口類型,即"InjectionDao injectionDao = new InjectionDaoImpl()", 這個(gè)其實(shí)是Java多態(tài)性(向上轉(zhuǎn)型)的一種應(yīng)用。在實(shí)現(xiàn)類處加@Repository注解,意思就是new InjectionDaoImpl(), 而在InjectionServiceImpl中定義屬性InjectionDAO injectionDAO就是將new出來的這個(gè)InjectionDaoImpl對(duì)象向上轉(zhuǎn)型為InjectionDao類型。
Spring中Autowired注入接口的幾個(gè)問題
1.Spring怎么知道注入哪個(gè)實(shí)現(xiàn)?
As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring's component scan enabled, Spring framework can find out the (interface, implementation) pair.
If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).
如果Spring配置了component scan,并且要注入的接口只有一個(gè)實(shí)現(xiàn)的話,那么spring框架可以自動(dòng)將interface于實(shí)現(xiàn)組裝起來。如果沒有配置component scan,那么你必須在application-config.xml(或等同的配置文件)定義這個(gè)bean。
可以理解為 在 application-service.xml 配置文件中聲明了 component-scan
<context:component-scan base-package="com.system.service.impl"/>
在 com.system.service 下有一個(gè)接口類,在 com.system.service.impl 下有一個(gè)接口對(duì)應(yīng)的實(shí)現(xiàn)類,且該實(shí)現(xiàn)類用 @Service 注解進(jìn)行了標(biāo)注。
在使用該接口類的時(shí)候,可以按照如下方式:
@Autowired private UserloginService userloginService;
以此來進(jìn)行自動(dòng)裝配。
2.需要@Qualifier和@Resource注解嗎?
Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation.
If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.
一旦一個(gè)接口有多個(gè)實(shí)現(xiàn),那么就需要每個(gè)特殊化識(shí)別并且在自動(dòng)裝載過程中使用@Qualifier和@Autowired一起使用來標(biāo)明。如果是使用@Resource注解,那么你應(yīng)該使用resource中屬性名稱來標(biāo)注@Autowired.
3.為什么@Autowired使用在interface上而不是實(shí)現(xiàn)類上?
Firstly, it is always a good practice to code to interfaces in general.
Secondly, in case of spring, you can inject any implementation at runtime.
A typical use case is to inject mock implementation during testing stage.
首先,一般使用接口是很常用并且有益的變成技術(shù)。
其次,在spring中,你可以在運(yùn)行過程中注入各種實(shí)現(xiàn)。一個(gè)很經(jīng)典的情況就是在測(cè)試階段,注入模擬的實(shí)現(xiàn)類。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談@RequestBody和@RequestParam可以同時(shí)使用
這篇文章主要介紹了@RequestBody和@RequestParam可以同時(shí)使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題
這篇文章主要介紹了mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Spring Boot集成Mybatis中如何顯示日志的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot集成Mybatis中如何顯示日志的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java實(shí)戰(zhàn)房屋租賃網(wǎng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)房屋租賃網(wǎng)站,大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11