SpringBoot中要實現bean的注入,依賴三種註解:
- @Autowired
- @Inject
- @Resource
1 @Autowired
在Spring Boot應用啓動時,Spring容器會自動裝載一個org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor處理器,當容器掃掃描到@Autowired註解時,就會在IoC容器就會找相應類型的Bean,並且實現注入。
1.1 @Autowired特點
- @Autowired爲Spring提供的註解,需要導入包org.springframework.beans.factory.annotation.Autowired
- @Autowired註解是按照類型(byType)裝配依賴對象,默認情況下它要求依賴對象必須存在,如果允許null值,可以設置它的required屬性爲false。如果我們想使用按照名稱(byName)來裝配,可以結合@Qualifier註解一起使用。
1.2 使用示例
在MVC模式中,控制層(controller)注入業務層(service)就需要用到@Autowired,如下所示(爲節省篇幅,多個類寫在一起):
// 控制層
@RestController
@RequestMapping(value = "/test")
public class CaseController {
@Autowired
private TestService testService;
@RequestMapping(value = "/autowired", method = RequestMethod.GET)
public int test() {
return testService.test();
}
}
// 業務層
@Service
public interface CaseService {
int test();
}
// 業務實現層
@Service(value = "testService")
public class CaseServiceImpl implements CaseService {
@Override
public int test(){
return 1;
}
}
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