Reputation: 3201
I use Java based configuration. When I have had only one UserRepository bean, all was working just fine. But when I added another one implementation of UserRepository, I got this error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'answerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.springapp.mvc.service.NewUserService com.springapp.mvc.service.AnswerService.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newUserService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.springapp.mvc.repository.UserRepository com.springapp.mvc.service.NewUserService.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.springapp.mvc.repository.UserRepository] is defined: expected single matching bean but found 2: [jpaUserRepository, jsonUserRepository]
Here is my configuration:
@EnableWebMvc
@Configuration
@ComponentScan("com.springapp.mvc")
@PropertySource("classpath:names.properties")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
}
And my service which is using one of UserService implementation:
@Service
public class NewUserService {
@Autowired
@Qualifier("jsonRepo")
public UserRepository userRepository;
// methods ommited
}
I tried to add @Qualifier
annotation to UserRepository userRepository
and to specific implementation:
@Repository("jsonRepo")
public class JsonUserRepository implements UserRepository {...}
But it doesn't work.
How can I fix this problem?
Upvotes: 0
Views: 1069
Reputation: 38328
Try this:
@Repository
@Qualifier("jsonRepo")
public class JsonUserRepository implements UserRepository {...}
Upvotes: 1