@Autowried和@Resource的区别(最详细)

前言

在Spring装在对象的时候,某个类的属性是其他类的对象,那我们使用注解如何去匹配呢?

一 @Autowried标签

1.1接口只有一个实现类

举个小例子吧:

public class StudentController {
	@Autowired
    private StudentService service;//只用@Autowired按照类型查询
}

StudentService是一个我写的interface 目的就是下面我们会引入多个实现类看看怎么办,先按部就班,只写一个实现类,Interface👇

public interface StudentService {
	//对 空着就行
}

实现类StudentServiceImpl1

@Service()
public class StudentServiceImpl1 implements StudentService {
	//也空着就ok
}

注意@Service()标签里面是空的,而上面我们@Autowried标签下 属性的名字是service,我们可以看到它和StudentServiceImpl1的名字是不一样的那么我们测试一下看看效果

       ApplicationContext f =new ClassPathXmlApplicationContext("applicationContext.xml");//立即加载
       StudentController sc =(StudentController) f.getBean("c");

断点测试👇
在这里插入图片描述
啥意思呢?也就是说这种情况下自动会去找相同类型的实现类,创建对象

1.2接口有多个实现类

正如所说,如果StudentService接口,有两个甚至更多实现类,而StudentController只需要某一个实现类的对象,比如这样👇

@Service("s1")
public class StudentServiceImpl2 implements StudentService {

}
@Service("s2")
public class StudentServiceImpl1 implements StudentService {

}

这时候我们就需要引入@Qualifier标签

public class StudentController {
	@Autowired
	@@Qualifier("s1")//他会默认的去找
    private StudentService service;//只用@Autowired按照类型查询
}

测试看看

       ApplicationContext f =new ClassPathXmlApplicationContext("applicationContext.xml");//立即加载
       StudentController sc =(StudentController) f.getBean("c");

在这里插入图片描述
所以通过Autowried和Qualifier再加Service这三个标签可以满足拿到我们需要的那个实现类对象

二 @Resource

2.1@Resource空参

@Resource这个标签是javaEE 包下的注解,所以很多人都说推荐使用它,因为它可以和Spring解耦
我们看看例子
我们还是两个实现类

@Service
public class StudentServiceImpl2 implements StudentService {

}
@Service
public class StudentServiceImpl1 implements StudentService {

}

首先我们@Resource啥都不写 我们想生成一个StudentServiceImpl2的对象

public class StudentController {
	@Resource()
    private StudentService studentServiceImpl2;//这里的属性名字必须和类名称一致
}

测试
在这里插入图片描述
没有问题

2.2@Service有参数@Resource无参

这种情况 我们还是拿StudentServiceImpl2👇

@Service("s1")
public class StudentServiceImpl2 implements StudentService {

}
@Service("s2")
public class StudentServiceImpl1 implements StudentService {

}

首先我们@Resource啥都不写 我们想生成一个StudentServiceImpl2的对象

public class StudentController {
	@Resource()
    private StudentService s2;//这里的属性名字必须和@Service("xx")中的xx名称一致
}

测试👇
在这里插入图片描述
没有问题是我们要的s2
哎 我们一样可以拿到对应的StudentServiceImpl2的对象

2.3@Resource(name=“xx”) @Service(“xx”)

@Service("s1")
public class StudentServiceImpl2 implements StudentService {

}
@Service("s2")
public class StudentServiceImpl1 implements StudentService {

}

首先我们@Resource啥都不写 我们想生成一个StudentServiceImpl2的对象

public class StudentController {
	@Resource(name="s2")
    private StudentService service;//随便叫,爱叫啥叫啥
}

也就是说 我们的属性名 已经可以放飞自我了 通过@Resource(name=“xx”)和@Service(“xx”)去控制
测试👇
在这里插入图片描述
也是没有问题的

2.4@Resource(type=xx.class)

@Resource也是可以通过指定的类型去获得相应的对象,这个意思👇

@Service
public class StudentServiceImpl2 implements StudentService {

}
@Service
public class StudentServiceImpl1 implements StudentService {

}

首先我们@Resource啥都不写 我们想生成一个StudentServiceImpl2的对象

public class StudentController {
	@Resource(type =StudentServiceImpl2.class)
    private StudentService service;//爱叫什么叫什么
}

测试
在这里插入图片描述
也是没有问题的

总结

 @Autowired 默认按照类型匹配 如果一个类的对象有多个 则默认按照属性名字进行匹配如
     @Autowired
     private StudentService studentServiceImpl1
    那么则会自动去创建StudentServiceImpl1这个类的对象

    1.@设置了@Qualifier("xx") 一定按照名字取匹配 匹配@Component("xx") 谁是xx类创建谁的对象
    2.
     2.1 @设置了@Resource(name="xxx") 默认按照xxx名字去匹配 如果没有设置name 自动按照属性名字匹配
     2.2@设置了@Resource(type =xxx.class)根据类型匹配

关于@Autowried和@Resource标签在Spring如何去使用就这些啦~ 纯原创 喜欢可以点赞加收藏哦~我会持续写一些Spring零碎的东西

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

商朝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值