当前位置:网站首页>@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
边栏推荐
猜你喜欢
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
Terms and concepts related to authority and authentication system
Chuantu microelectronics ca-if1051 can-fd transceiver
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
海思3559 sample解析:vio
Full range of isolator chips with integrated isolated power supply
realsense d455 semantic_ Slam implements semantic octree mapping
随机推荐
双三次差值bicubic
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
php入门基础记录
Runtime - Methods member variable, cache member variable
@Resource和@Autowired注解的不同,为什么推荐@Resource?
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
1742. 盒子中小球的最大数量
Six causes of PCB disconnection 2021-10-20
C#获取exe的版本号-文件版本and程序集版本
【视频】ffplay 使用mjpeg格式播放usb摄像头
OAuth 2.0 one click login
Runtime——methods成员变量,cache成员变量
Bicubic difference
力扣76题,最小覆盖字串
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
ts环境搭建
How to use ad wiring for PCB design?
2160. 拆分数位后四位数字的最小和
VSCode很好,但我以后不会再用了