当前位置:网站首页>Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
2022-07-31 07:09:00 【try harder c.】
前言
1、什么是byName和byType
简单来说,byNameIt is to match according to the variable namebean的id属性,而byTypeIt is matched according to the variable typebean的class属性.实例说明:
<bean id="userService" class="com.test.UserServiceImpl"></bean>
@Autowired
private UserService userService;
此处byNameJust take the variable nameuserService
去匹配IOC容器的id为userService
的bean,而byTypeJust take the variable typeUserService
去匹配IOC容器的id为com.test.UserService.UserServiceImpl
的bean,Here can be matched successfully becauseUserServiceImpl
是UserService
的实现类.
2、@Resource与@Autowired都是用来自动装配bean的.
一、@Resource
1、java中自带的注解 (javax.annotation.Resource)
2、默认按照名称(byName)方式进行bean装配,也可以按类型(byType)装配
3、@Resource的作用范围:类、成员变量、方法
4、@Resource的使用
步骤:@Resource默认通过byName注入,如果没有匹配则通过byType注入.
@Service
public class UserServiceImpl1 implements UserService
@Service
public class UserServiceImpl2 implements UserService
@Resource
private UserService userService;
First pass by defaultbyName匹配,变量名userService
无法匹配IOCany of the containersid(因为这里UserService
有两个实现类,There are corresponding twobean,即userServiceImpl1
和userServiceImpl2
),于是再通过byType匹配,发现类型UserService
The implementation classes are two,仍然无法确定,于是报错.
It will be mentioned at this [email protected]两个重要的属性:name和type,They are used to specify explicitlybyName和byType方式.
Displays the designation to match thatbean时,主要有以下四种情况:
// 1. 默认方式:byName That is, the designation is not displayed
@Resource
private UserService userDao;
// 2. 显示指定byName
@Resource(name="userService")
private UserService userService;
// 3. 显示指定byType
@Resource(type=UserService.class)
private UserService userService;
// 4. 显示指定byName和byType
@Resource(name="userService",type=UserService.class)
private UserService userService;
①既没指定name属性,也没指定type属性:默认通过byName方式注入,如果byName匹配失败,则使用byType方式注入(That is the example above)
②指定name属性:通过byName方式注入,Put the variable name and IOC容器中的id去匹配,If the match fails, an error will be reported
③指定type属性:通过byType方式注入,在IOCMatches the corresponding type in the container,If there is no match or if more than one match is found, an error will be reported
④同时指定name属性和type属性:在IOC容器中匹配,A match for both name and type succeeds,Otherwise, an error will be reported.
二、@Autowired
1、Spring自带的注解(org.springframework.beans.factory.annotation.Autowired)
2、默认按照类型(byType)方式进行bean装配,Also by name(byName)装配,需要结合@Qualifier如:@Autowired @Qualifier("manImpl")
3、Assembled by defaultbean对象必须在IOC容器中存在,If it does not exist, an error will be reported but normal use will not be affected;若允许bean对象不存在,可设置@Autowired(required = false)
.That is to say, ignore the current injectionbean,如果有直接注入,没有则跳过,也不会报错了
4、@Autowired的作用范围:成员变量、构造器、方法、参数、注解
5、@Autowired的使用
步骤:@Autowird默认的注入方式为byType,也就是根据类型匹配,当有多个实现时,则通过byName注入,也可以通过配合@Qualifier注解来显式指定name值,指明要使用哪个具体的实现类.
@Service
public class UserServiceImpl1 implements UserService
@Service
public class UserServiceImpl2 implements UserService
@Autowired
private UserService userService;
根据上面的步骤,can be easily judged,Using this directly will result in an error.
原因:首先通过byType注入,判断UserService
Types have two implementations,Can't be sure which one it is,于是通过byName方式,这里的变量名userService
也无法匹配IOC容器中id(referred to hereuserServiceImpl1
和userServiceImpl2
),于是报错.
(注意:通过注解注入到IOC容器的idThe value defaults to its class name in lower case,比如上面的UserServiceImpl1和UserServiceImpl2类bean的id默认为userServiceImpl1和userServiceImpl2)
解决方案:
// 方式一:改变变量名
//将原来的userservice改名为userServiceImpl1,This will match the corresponding uniquebean了
@Autowired
private UserService userServiceImpl1;
// 方式二:配合@Qualifier注解来显式指定name值,This will match the corresponding uniquebean了
@Autowired
@Qualifier(value = "userServiceImpl2")
private UserService userService;
// 方式三:配合@Primaryannotation to specify which one to autowirebean
//加上@Primary,匹配bean时,该beanwill be selected first.
@Service
@Primary
public class UserServiceImpl1 implements UserService
三、总结
1、@Resource与@Autowired对比
2、@Autowired装配流程
3、@Resource装配流程
边栏推荐
- 第十六章:构建n(5,7)阶素数幻方
- 浅层了解欧拉函数
- Oracle入门 12 - Linux 磁盘分区及LVM实战
- Analysis of the principle and implementation of waterfall flow layout
- Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
- 选择排序法
- Database Principles Homework 2 — JMU
- How to choose a suitable UI component library in uni-app
- 高并发与多线程之间的难点对比(容易混淆)
- 对van-notice-bar组件定义内容进行设置
猜你喜欢
随机推荐
MySQL系列一:账号管理与引擎
对van-notice-bar组件定义内容进行设置
DDL+DML+DQL
项目-log4j2排查问题
MySql的安装配置超详细教程与简单的建库建表方法
快速傅里叶变换(FFT)
How to use repeating-linear-gradient
Moment.js常用方法
QFileInfo常规方法
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
银河麒麟高级服务器v10 sp1 手动加载Raid卡驱动
全网首发!ADK To Win11PE(1)中文+包
高并发与多线程之间的难点对比(容易混淆)
Redux状态管理
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
【Star项目】小帽飞机大战(八)
搭建zabbix监控及邮件报警(超详细教学)
服务器和客户端信息的获取
mysql的下载及安装使用
360推送-360推送工具-360批量推送工具