【SpringBoot注解-3】Bean注入相关注解

}




[]( )1.2 常见异常

-------------------------------------------------------------------



异常场景一:接口没有实现类,启动Spring Boot应用会报如下错:



Description:

Field interfaceTest in com.ui.InterfaceController required a bean of type ‘com.ui.InterfaceTest’ that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type ‘com.ui.InterfaceTest’ in your configuration.

Process finished with exit code 1




从报错信息中可以看到,在@Autowired中添加required = false即可:



@RestController

public class InterfaceController {

@Autowired(required = false)

private InterfaceTest interfaceTest;



@RequestMapping(value = "/test", method = RequestMethod.GET)

public int getCaseList() {

    return interfaceTest.getCount();

}

}




项目可以正常启动,当然调用这个controller时,会报出空指针异常:



java.lang.NullPointerException: null

at com.ui.InterfaceController.get(TestServiceImpl.java:23) ~[classes/:na]

at com.ui.InterfaceController.get(TestController.java:18) ~[classes/:na]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]



异常场景二:接口存在多个实现类  

当接口存在**存在多个实现类**时,需要做以下特殊处理:  

在@Autowired里将required属性置为 false,即告诉SpringBoot匹配不到相应 Bean 时也不要报错,再在\*\*@Qualifier\*\*中指定要注入的实现类(与想注入的类名相同,首字母小写),如下:



// TestService第二个实现类

@Service(value = “testService”)

public class testServiceImplSecond implements testService {

@Override

public int test(){

	return 2;

}

}

// 接口层注入testService,指定第二个实现类

@RestController

@RequestMapping(value = “/test”)

public class CaseController {

@Autowired(required = false)

@Qualifier("testServiceSecond")

private TestService testService;



@RequestMapping(value = "/autowired", method = RequestMethod.GET)

public int test() {

    return testService.test();

}

}




[]( )2 @Resource

======================================================================



@Resource作用与@Autowired大致相同,同时有一些不同:



*   @Autowired是Spring的注解,@Resource是J2EE的注解,根据导入注解的包名就可以知道。  

    \-@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配

*   Spring属于第三方的,J2EE是Java自己的东西。因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。

*   @Autowired可以作用在constructor、method、parameter、field、annotation\_type上,@Resource可以作用method、field、type上。



示例如下:



@Service

public class School{

@Resource(name = "teacher")

private Teacher teacher;

@Resource(type = Student.class)

private Student student;

public String toString(){

    return teacher + "\n" + student;

}

}




规则如下:



*   @Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配。

*   指定了name或者type则根据指定的类型去匹配bean。

*   指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都会报错。



[]( )3 @Inject

====================================================================



*   @Inject是JSR330 (Dependency Injection for Java)中的规范,需要导入javax.inject.Inject jar包 ,才能实现注入。

*   @Inject可以作用constructor、method、field上。

*   @Inject是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Named;



简单示例:



@Inject

@Named(“BMW”)

private Car car;




@Named 的作用类似 @Qualifier



 



  



本文转自 [https://blog.youkuaiyun.com/mu\_wind/article/details/102684511]( ),如有侵权,请联系删除。


## 最后

**码字不易,觉得有帮助的可以帮忙点个赞,让更多有需要的人看到**

又是一年求职季,在这里,我为各位准备了一套Java程序员精选高频面试笔试真题,来帮助大家攻下BAT的offer,题目范围从初级的Java基础到高级的分布式架构等等一系列的面试题和答案,用于给大家作为参考

以下是部分内容截图
![架构面试专题及架构学习笔记导图.png](https://img-blog.csdnimg.cn/img_convert/2cb42362aa4aa8d8c4cce34ab938aa46.webp?x-oss-process=image/format,png)
 [https://blog.youkuaiyun.com/mu\_wind/article/details/102684511]( ),如有侵权,请联系删除。


## 最后

**码字不易,觉得有帮助的可以帮忙点个赞,让更多有需要的人看到**

又是一年求职季,在这里,我为各位准备了一套Java程序员精选高频面试笔试真题,来帮助大家攻下BAT的offer,题目范围从初级的Java基础到高级的分布式架构等等一系列的面试题和答案,用于给大家作为参考

以下是部分内容截图
[外链图片转存中...(img-KjywE8lS-1718783538630)]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值