I have a generic repository interface called UserRepository. I then have an interface which extends from that called MyUserRepository. It deals with an MyUser class which extends User.
I also have a Service Interface called UserService and a class called MyUserServiceImpl.
The service wants an instance of the UserRepository and I though I could use some sort of annotation like @Qualifier but it doesn't work.
@NoRepositoryBean
public interface UserRepository <T extends User> extends JpaRepository<T, Long>{
<S extends T> S findByLoginName(String loginName);
<S extends T> S saveAndFlush(User user);
}
@Repository
@Qualifier("myUserRepository")
public interface MyUserRepository extends UserRepository<MyUser> {
}
public interface UserService {
public List<User> getUsers();
}
@Service
public class MyUserServiceImpl implements UserService {
@Autowired
@Qualifier("myUserRepository")
private UserRepository<User> userRepository;
@Override
public List<User> getUsers() {
....
}
}
APPLICATION FAILED TO START
Description:
Parameter 0 of constructor in com....services.MyUserServiceImpl required a bean of type 'com....repositories.UserRepository' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com....repositories.UserRepository' in your configuration.
@Qualifier
annotation is used only when calling a bean already created. So you shouldn't call on class head, you might name it @Repository("myUserRepository")
and call it on after @Autowired
@Qualifier("myUserRepository")
No need to have a qualifier in your case.
@Repository
public interface MyUserRepository extends UserRepository<MyUser> {
}
Auto-wire the repository as :
@Service
public class MyUserServiceImpl implements UserService {
@Autowired
private UserRepository<User> userRepository;
...
@Qualifier is used with @Autowired annotation. By default @Autowired will inject beans based on types.When you have multiple beans of same types then @Qualifier helps to resolve the conflict. In your case using annotation @Repository will do you job. Also in your UserRepository interface , you have to supply the Id class along with JPA entity class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.