Spring annotations @ Autowired and @ Resource

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Resource(name="stringRedisTemplate")
private ValueOperations<String,String> valOpsStr;

@Autowired
private RedisTemplate<Object, Object> redisTemplate;

@Resource(name="redisTemplate")
private ValueOperations<Object,Object> valOps;

found such a piece of code while reading, that is, what I want to ask is, [@ Autowired annotations StringRedisTemplate, automatically, but why can stringRedisTemplate be specified when the @ Resource annotation uses the name attribute to specify a name?] what is the principle of this, and this will not lead to the creation of a bean with the same name?

Mar.23,2021

@ Autowired literally means automatic injection, while @ Resource, needs to inject a named bean,. Their lookup mechanism is different. @ Autowired needs to scan items and find relevant Bean automatic injection. It is automatic, and you don't have to worry about how to inject it. @ Resource needs to manually specify a Bean, if it is a spring boot project, you need to configure a configuration file, and use @ Bean ('xxx') to return the named Bean, if it is not spring boot, then you need to configure it in xml. One is automatic and the other is manual, so there is no conflict.

Menu