欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類的問(wèn)題

 更新時(shí)間:2022年10月01日 09:39:58   作者:發(fā)則韓  
這篇文章主要介紹了Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@Autowired加到接口上但獲取的是實(shí)現(xiàn)類

問(wèn)題

Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類?

?? ?/* 類 @Controller注解,會(huì)在spring容器中實(shí)例化對(duì)象 */
?? ?@Controller
?? ?public class UserContoller{
?? ??? ?@Autowired?? ??? ?// 先按類型找,然后按id為屬性名去找
?? ??? ?private UserService userService;
?? ??? ?//為什么他會(huì)拿到userServiceImpl?
?? ??? ?// @Autowired會(huì)幫你按UserService的類型去容器中找唯一bean對(duì)象
?? ??? ?// 1、容器沒(méi)有該類型的對(duì)象:報(bào)錯(cuò)
?? ??? ?// 2、容器中有該類型的唯一bean對(duì)象,就將該唯一bean對(duì)象賦值給該屬性
?? ??? ?///3、容器中有多個(gè)【兩個(gè)及以上】該類型的唯一bean對(duì)象,
?? ??? ?// ? ? 它會(huì)再根據(jù)該屬性名去容器中找,
?? ??? ?// ? ? 看看容器中的哪個(gè)bean對(duì)象的id值和該屬性名一致,
?? ??? ?// ? ? 如果有,就將容器中該對(duì)象賦值給該屬性,如果沒(méi)有報(bào)錯(cuò)。
?? ?}?? ?
?? ?/* 接口 ?*/
?? ?public interface UserService{}
?? ?
?? ?/* 類 ?@Service注解,會(huì)在spring容器中實(shí)例化對(duì)象 */
?? ?@Service
?? ?public class UserServiceImpl implements UserService{}

為什么他會(huì)拿到userServiceImpl?

@Autowired先按類型找,然后再按id為屬性名去找

他會(huì)幫你按UserService的類型去容器中找唯一bean對(duì)象

  • 1.容器沒(méi)有該類型的對(duì)象:報(bào)錯(cuò)
  • 2.容器中有該類型的唯一bean對(duì)象,就將該唯一bean對(duì)象賦值給該屬性
  • 3.容器中有多個(gè)【兩個(gè)及以上】該類型的唯一bean對(duì)象,

它會(huì)再根據(jù)該屬性名去容器中找,看看容器中的哪個(gè)bean對(duì)象的id值和該屬性名一致,如果有,就將容器中該對(duì)象賦值給該屬性,如果沒(méi)有報(bào)錯(cuò)。

然后通過(guò)多態(tài)的向上轉(zhuǎn)型就賦值成功。等價(jià)于之前手動(dòng)賦值

UserService userService = new UserServiceImpl();

@Autowired一個(gè)接口有多個(gè)實(shí)現(xiàn)類

@Autowired是spring的注解,默認(rèn)使用的是byType的方式向Bean里面注入相應(yīng)的Bean。

例如

@Autowired
private UserService userService;

這段代碼會(huì)在初始化的時(shí)候,在spring容器中尋找一個(gè)類型為UserService的bean實(shí)體注入,關(guān)聯(lián)到userService的引入上。

但是如果UserService這個(gè)接口存在多個(gè)實(shí)現(xiàn)類的時(shí)候,就會(huì)在spring注入的時(shí)候報(bào)錯(cuò),具體如下:

public class UserService1 implements UserService
public class UserService2 implements UserService

當(dāng)存多個(gè)UserService的實(shí)現(xiàn)類時(shí),錯(cuò)誤信息如下:

2016-08-05 14:53:53,795 ERROR [org.springframework.test.context.TestContextManager] - <Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@14a2f921] to prepare test instance [UserServiceTest@3c87521]>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private yjc.demo.service.UserService UserServiceTest.userService; nested exception is he[yjc.demo.service.UserService] is defined: expected single matching bean but found 2: userService1,userService2

拋出了org.springframework.beans.factory.BeanCreationException,而原因是注入的時(shí)候發(fā)現(xiàn)有2個(gè)匹配的bean,但是不知道要注入哪一個(gè):expected single matching bean but found 2: userService1,userService2

那么如何應(yīng)對(duì)多個(gè)實(shí)現(xiàn)類的場(chǎng)景呢,看一下代碼:

@Autowired
private UserService userService1;
?
@Autowired
private UserService userService2;
?
@Autowired
@Qualifier(value = "userService2")
private UserService userService3;
?
@Test
public void test(){
? ? ? ? ?System.out.println(userService1.getClass().toString());
? ? ? ? ?System.out.println(userService2.getClass().toString());
? ? ? ? ?System.out.println(userService3.getClass().toString());
}

運(yùn)行結(jié)果:

class yjc.demo.serviceImpl.UserService1
class yjc.demo.serviceImpl.UserService2
class yjc.demo.serviceImpl.UserService2

運(yùn)行結(jié)果成功,說(shuō)明了2種處理多個(gè)實(shí)現(xiàn)類的方法:

1.變量名用userService1,userService2,而不是userService。

通常情況下@Autowired是通過(guò)byType的方法注入的,可是在多個(gè)實(shí)現(xiàn)類的時(shí)候,byType的方式不再是唯一,而需要通過(guò)byName的方式來(lái)注入,而這個(gè)name默認(rèn)就是根據(jù)變量名來(lái)的。

2.通過(guò)@Qualifier注解來(lái)指明使用哪一個(gè)實(shí)現(xiàn)類,實(shí)際上也是通過(guò)byName的方式實(shí)現(xiàn)。

由此看來(lái),@Autowired注解到底使用byType還是byName,其實(shí)是存在一定策略的,也就是有優(yōu)先級(jí)。優(yōu)先用byType,而后是byName。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論