@Autowire、@Resource、@Qualifier的区别

一、简单说明

这三个注解的作用都是用来注入依赖对象

@Autowire

@Autowired 接口只能有一个实现类,通过byType方式注入
默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false)

@Resource

@Resource 接口可以有多个实现类,先通过byName方式进行匹配,若匹配失败再通过byType方式注入

@Qualifier

@Qualifier 接口可以有多个实现类,可以按实现类的类名进行注入

二、场景示例

1、场景一(接口只有一个实现类)

Person接口

package com.study.annotationdemo.service;

/**
 * @description Person接口
 * @author zzy
 * @date 2021年08月09日 16:43
 */
public interface Person {
}

StudentImpl实现类

package com.study.annotationdemo.service.impl;

import com.study.annotationdemo.service.Person;
import org.springframework.stereotype.Service;

/**
 * @description Person 实现类-student
 * @author zzy
 * @date 2021年08月09日 16:45
 */
@Service("sImpl")
public class StudentImpl implements Person {
}

Controller

package com.study.annotationdemo.controller;

import com.study.annotationdemo.service.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @description
 * @author zzy
 * @date 2021年08月09日 16:45
 */
@Controller
public class DemoController {

    @Autowired
    private Person person1;

    @Resource(name = "sImpl") // service注解标注的名字
    private Person person2;

    @Qualifier("StudentImpl") // 注意是实现类的类名
    private Person person3;
}

接口只有一个实现类时,三个注解都可以使用

2、场景二(接口有多个实现类)

在上面的场景的基础上再添加一个教师实现类

package com.study.annotationdemo.service.impl;

import com.study.annotationdemo.service.Person;
import org.springframework.stereotype.Service;

/**
 * @description Person 实现类-teacher
 * @author zzy
 * @date 2021年08月09日 17:08
 */
@Service("tImpl")
public class TeacherImpl implements Person {
}

在这里插入图片描述

此时会发现@Autowired注入的对象会报错,因为@Autowired通过byType来进行注入,这时Person接口有了多个实现类,它无法判断注入哪个。
@Resource@Qualifier仍然适用

Controller

package com.study.annotationdemo.controller;

import com.study.annotationdemo.service.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @description
 * @author zzy
 * @date 2021年08月09日 16:45
 */
@Controller
public class DemoController {

//    @Autowired
//    private Person person1;

    @Resource(name = "sImpl") // service注解标注的名字
    private Person person2;

    @Qualifier("StudentImpl") // 注意是实现类的类名
    private Person person3;

    @Resource(name = "tImpl") // service注解标注的名字
    private Person person4;

    @Qualifier("TeacherImpl") // 注意是实现类的类名
    private Person person5;
}

可以通过名字来寻找实现类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值