SpringBoot中要实现bean的注入,依赖三种注解:
在Spring Boot应用启动时,Spring容器会自动装载一个org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor处理器,当容器扫扫描到@Autowired注解时,就会在IoC容器就会找相应类型的Bean,并且实现注入。
在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;
}
}
异常场景一:接口没有实现类,启动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();
}
}
@Resource作用与@Autowired大致相同,同时有一些不同:
示例如下:
@Service
public class School{
@Resource(name = "teacher")
private Teacher teacher;
@Resource(type = Student.class)
private Student student;
public String toString(){
return teacher + "\n" + student;
}
}
规则如下:
简单示例:
@Inject
@Named("BMW")
private Car car;
@Named 的作用类似 @Qualifier