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

时间:2022-07-25
本文章向大家介绍【SpringBoot注解-3】Bean注入相关注解,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

常见注解总结之Bean注入

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