[英]How to use @Qualifier with Service and Repository
I have a generic repository interface called UserRepository. 我有一个称为UserRepository的通用存储库接口。 I then have an interface which extends from that called MyUserRepository.
然后,我有了一个扩展自MyUserRepository的接口。 It deals with an MyUser class which extends User.
它处理扩展用户的MyUser类。
I also have a Service Interface called UserService and a class called MyUserServiceImpl. 我也有一个名为UserService的服务接口和一个名为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. 该服务需要UserRepository的实例,尽管我可以使用@Qualifier之类的注释,但是它不起作用。
@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.
com .... services.MyUserServiceImpl中的构造函数的参数0需要找不到类型为'com .... repositories.UserRepository'的bean。
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
注入点具有以下注释:-@ org.springframework.beans.factory.annotation.Autowired(required = true)
Action: 行动:
Consider defining a bean of type 'com....repositories.UserRepository' in your configuration.
考虑在配置中定义类型为“ com .... repositories.UserRepository”的bean。
@Qualifier
annotation is used only when calling a bean already created. @Qualifier
批注仅在调用已创建的bean时使用。 So you shouldn't call on class head, you might name it @Repository("myUserRepository")
and call it on after @Autowired
@Qualifier("myUserRepository")
因此,您不应调用类的头,可以将其命名为
@Repository("myUserRepository")
,然后在@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. @Qualifier与@Autowired注释一起使用。 By default @Autowired will inject beans based on types.When you have multiple beans of same types then @Qualifier helps to resolve the conflict.
默认情况下,@ Autowired将根据类型注入bean。当您具有多个相同类型的bean时,@ Qualifier可以帮助解决冲突。 In your case using annotation @Repository will do you job.
在您的情况下,使用注释@Repository可以完成您的工作。 Also in your UserRepository interface , you have to supply the Id class along with JPA entity class.
同样在UserRepository接口中,您必须提供Id类以及JPA实体类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.