why
The StringRedistemplate object is injected into the project as follows:
@Autowired
private StringRedisTemplate redisTemplate;
Then it says an error:
Field redisTemplate in com.xxx.api.controller.XxxController required a single bean, but 2 were found:
- getRedisTemplate: defined by method 'getRedisTemplate' in class path resource [com/xxx/config/RedisConfig.class]
- stringRedisTemplate: defined by method 'stringRedisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class]
@Autowired is either injected by type or declared @Qualifier(” beanID “), which has nothing to do with the field name I defined as redisTemplate. Other
but the project is so written, nor the @ the Qualifier is not an error (and I didn’t complain before use), the only difference is the field name is different, other places are stringRedisTemplate, I changed, really good. This is clearly a metaphysical operation, if you follow the previous interpretation of @Autowired.
why
@Autowired annotation injection rule:
after some code tests, the Autowired default by Type, if the same Type to find multiple beans, then, and according to the Name way comparison, if there are more than one, then an exception is reported.
According to the error prompt: StringRedisTemplate this type of bean in container pool has two redisTemplate, StringRedisTemplate (our own projects, configured with a spring boot automatically configure also give I added a)
I defined the field name is redisTemplate: no, just the StringRedisTemplate is some, so there is no problem!
To solve
There are many solutions:
- field is modified for StringRedistemPlate to specify the injected bean using @Qualifier. In some cases, add @primary [not recommended]
END
I had memorized the infusion process deeply until I came across it. Some items in the notes didn’t impress me as being important, and I kept a summary in mind, yet I couldn’t discover its significance until I encountered the problem.
Read More:
- Springboot project startup exception – required a single bean, but 2 were found
- Error creating bean with name ‘helloController‘: Injection of autowired dependencies failed;
- Datasource bean injection failed, with startup error
- error Newline required at end of file but not found eol-last
- Could NOT find CUDNN: Found unsuitable version “..“, but required is at least “6“
- Field userrepository in com.example.demo2.service.imp.UserServiceImp required a bean of type ‘com.ex
- Info:Memory module [DIMM] needs attention: Single-bit warning error rate exceeded, Single-bit fai…
- configure: error: libpam required but missing
- Registry key Error: Java version has value ‘1.8‘, but ‘1.7‘ is required
- Solution of idea using @ Autowired annotation to report errors
- Start error: field xmapper in com.xxx.service . impl.xxxServiceImpl required a bean of type
- Solve the problem of jdk8 after win7 is installed. Has value ‘1.8’, but ‘1.7’ is required
- TypeError: Required argument ‘mat‘ (pos 2) not found
- Expected indentation of 6 spaces but found 10
- Solution of redisconnection factory in springboot2 not Autowired
- Python about typeerror: required argument ‘mat’ (POS 2) not found error resolution
- error Expected indentation of 2 spaces but found 8
- RuntimeError: Found dtype Double but expected Float”
- Solve the runtimeerror in RNN: expected scalar type long but found float error
- RuntimeError: expected scalar type Double but found Float