简体   繁体   English

如何在服务和存储库中使用@Qualifier

[英]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.

相关问题 如何在Hibernate中使用@Qualifier - How to use @Qualifier in Hibernate 如何在 Controller、服务和存储库模式中使用 DTO - How to use DTOs in the Controller, Service and Repository pattern 如何使用“@Qualifier”动态指定参数? - how to use '@Qualifier' dynamically specifying parameters? 如何在mapstruct中将@Qualifier或@Named与@AfterMapping一起使用? - How to use @Qualifier or @Named with @AfterMapping in mapstruct? 如何使用限定词来确定类的字段? - How to use qualifier to decide the fields of a class? 如何对使用存储库的服务层进行春季单元测试? - How to do a Spring Unit Test of a Service layer that use Repository? 如何根据属性文件中的设置强制服务使用特定的存储库? - How, depending on the settings in the property file, force service to use a particular repository? 如何使用Java CDI在非默认类上使用限定符 - How to use qualifier on non default class with java cdi 如何使用 dagger 限定符注解来提供不同的 OkHttpClient 构建器? - How to use dagger qualifier annotations to provide different OkHttpClient builders? 当 class 注入限定符时,如何在单元测试中使用不同的实现 - How to use different implementation in unit test when class injects with Qualifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM