【Spring】The difference between @Resource, @Autowired and @Qualifer

 The difference between @Resource, @Autowired and @Qualifer The
above three annotations in Spring can be used to implement dependency injection, but it is important to know the difference and connection of the three when using
@Resource. For
example:

@Resource
CityDao citydao;

[1] First, according to the class name, lowercase the first letter of the class name to get cityDao, and then go to the Spring configuration file to find the bean whose id is cityDao, that is, name matching

[2] If there is no bean with the id of cityDao in the Spring configuration file, then match according to the type, and look for the bean with the class of CityDao in the configuration file

Precautions for using @Autowired
Example:

@Autowired
CityDao cityDao;

[1] Matching directly according to the type, Spring will go to the configuration file to find the bean whose class is CityDao

Precautions for use of @Qualifer
Example:

@Autowired
@Qualifer("cityDao2")
CityDao cityDao;

[1] @Qualifer cannot be used alone, otherwise a null pointer error will occur

[2] It needs to be used in conjunction with @Autowired, and the bean with the id of cityDao2 will be found from the Spring configuration file, that is, the name will be matched.
Original link: (192 messages) [Spring] The difference between @Resource, @Autowired and @Qualifer_ Yangyang's Blog - CSDN Blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324147674&siteId=291194637