当前位置:网站首页>@The difference between resource and @autowired annotation, why recommend @resource?
@The difference between resource and @autowired annotation, why recommend @resource?
2022-06-25 07:49:00 【jack_ bob】
2 Both can be used to assemble bean, When the interface has only a single implementation class , The two annotations have the same decoration effect , They can replace each other , Not affecting use .
1.
@Autowired Assembly by type by default ( This note is industry spring Of ), By default, dependency objects must exist , If you want to allow null value , You can set its required The attribute is false, Such as :@Autowired(required=false) , If we want to use name assembly, we can combine @Qualifier Use annotations
@Resource( This note belongs to J2EE Of ), By default, assemble by name , The name can be given by name Property to specify , If not specified name attribute , When comments are written on fields , By default, the field name is used to search the installation name , If the note is written in setter Method, the property name is used for assembly by default . When no match can be found for the name bean Assembly according to type . But here's the thing , If name Property once specified , Just assemble by name .
When an interface has 2 Implementation cases :
give an example :
An interface :
public interface UserDao {
public void insert();
}
Realization 1:
@Repository("userJdbcDao")
public class UserJdbcDao implements UserDao{
@Override
public void insert() {
System.out.println(" I am a -jdbc-Dao- Implement the insert operation ");
}
}
Realization 2:
@Repository("userJndiDao")
public class UserJndiDao implements UserDao{
@Override
public void insert() {
System.out.println(" I am a -jndi-Dao- Implement the insert operation ");
}
}
service class :
@Service("userService")
public class UserService {
@Autowired
@Qualifier("userJdbcDao")
private UserDao userDao=null;
public void createUser(){
userDao.insert();
}
}
1. here ide Will detect what you injected Dao It's not the only one , May adopt @Qualifier Use with annotations , Specify which... You want to inject dao
2. It can also be used on classes that implement interfaces :
@Primary In case of conflict, which implementation class should be used first
Recommended :@Resource:
Implementation class :@Repository(“userJdbcDao”) and service Of @Resource(name=“userJdbcDao”) Be consistent
UserJdbcDao Different implementation class names do not affect
@Repository("userJdbcDao") // life beanId
public class UserJdbcDao implements UserDao{
@Override
public void insert() {
System.out.println(" I am a -jdbc-Dao- Implement the insert operation ");
}
}
@Service("userService")
public class UserService {
@Resource(name="userJdbcDao")
private UserDao userDao=null;
public void createUser(){
userDao.insert();
}
}
@SpringBootTest
class TestApplicationTests {
@Resource(name="userService")
private UserService userService;
@Test
public void testcreateUser(){
userService.createUser();
}
}
Code :
Test code source address
The article comes from It Lao Qi 001
边栏推荐
- 国外LEAD域名邮箱获取途径
- Requirements for Power PCB circuit board design 2021-11-09
- GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items
- realsense d455 semantic_ Slam implements semantic octree mapping
- 一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
- 剑指 Offer II 027. 回文链表
- 点云智绘在智慧工地中的应用
- What are the benefits of reserving process edges for PCB production? 2021-10-25
- Tips on how to design soft and hard composite boards ~ 22021/11/22
- Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field
猜你喜欢

Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks

OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线

C#中如何调整图像大小

OAuth 2.0一键登录那些事

一次弄清楚 Handler 可能导致的内存泄漏和解决办法

年后求职找B端产品经理?差点把自己坑惨了......

海思3559 sample解析:vio

Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification

Tips on how to design soft and hard composite boards ~ 22021/11/22

饮食干预减轻癌症治疗相关症状和毒性
随机推荐
海思3559 sample解析:vio
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
c# winform panel自定义图片和文字
Pit encountered by pytorch: why can't l1loss decrease during model training?
NSIS 静默安装vs2013运行时
Misunderstanding of switching triode
OpenMP入门
Storage of Galileo broadcast ephemeris in rtklib-b33
LeetCode_哈希表_中等_454.四数相加 II
函数模板_类模板
useMemo模拟useCallback
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
国外LEAD域名邮箱获取途径
VSCode很好,但我以后不会再用了
Different paths ii[dynamic planning improvement for DFS]
Keil and Proteus joint commissioning
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
Advantages and differences of three kinds of vias in PCB 2021-10-27
基于STM32MP157调试MIPI-DSI屏幕