我的自定义存储库实现中的CrudRepository [英] CrudRepository inside my custom repository implementation

查看:169
本文介绍了我的自定义存储库实现中的CrudRepository的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的自定义实现中获取对我的存储库接口( UserRepository )的引用,该引用扩展了 CrudRepository UserRepositoryExtensionImpl ),以便访问Spring JPA提供的所有方法。


$ b Crud扩展:

  @Repository 
public interface UserRepository扩展CrudRepository< User,String>,UserRepositoryExtension< RosterUser> {
...任何自定义Spring JPA方法...
}

扩展接口:

  @Repository 
public interface UserRepositoryExtension< T> {
public T put(T实体);



$ b $ p $自定义实现:

$ b $ b pre> public class UserRepositoryExtensionImpl implements UserRepositoryExtension< User> {

UserRepository userRepository;

@Autowired
public UserRepositoryExtensionImpl(UserRepository userRepository){
this.userRepository = userRepository;
}

@Override
public User put(User user){
System.out.println(user +was put);
// ...把​​逻辑放在这里
返回null;
} ...
}

然而,我无法注入 UserRepository ,因为存在循环依赖关系(假设 UserRepository 扩展了我的 UserRepositoryImpl )。我收到以下错误:

  org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为'userRepositoryImpl'的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用? 

一个可能但不太理想的解决方案是注入和 EntityManager 转换为 UserRepositoryImp ,但在这种情况下,我无法访问由 CrudRepository 提供的任何Spring JPA方法。 code>,或者我可能在UserRepository中创建的任何其他方法。



有关如何解决此问题的任何建议?

任何帮助将不胜感激。编辑:正如@ shelley的回答中提到的,我能够通过进行3次更改来解决这个问题:

/ p>



SUCCESS!

解决方案

一些小的事情需要改变才能使用:


  • 删除 @存储库自定义存储库接口的注释( UserRepositoryExtension )。

  • 定制存储库实现实际上应该命名为< StandardRepository> Impl ,而不是< CustomRepository> Impl 。在你的代码示例中,这应该是 UserRepositoryImpl ,而不是 UserRepositoryExtensionImpl



I am attempting to get a reference to my repository interface (UserRepository) that extends CrudRepository within my custom implementation (UserRepositoryExtensionImpl) in order to gain access to all the methods provided by Spring JPA.

Crud Extension:

@Repository
public interface UserRepository extends CrudRepository<User, String>, UserRepositoryExtension<RosterUser> {
    ...any custom spring JPA methods...
}

Extension Interface:

@Repository
public interface UserRepositoryExtension <T> {
   public T put(T entity);
}

Custom Implementation:

public class UserRepositoryExtensionImpl implements UserRepositoryExtension<User> {

    UserRepository userRepository;

    @Autowired
    public UserRepositoryExtensionImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public User put(User user) {
        System.out.println(user + "was put");
        // ...put logic here
        return null;
    }...
}

However, I am unable to inject UserRepository since a circular dependency exists (given that UserRepository extends the interface implemented by my UserRepositoryImpl). I am getting the following error:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name '    userRepositoryImpl': Requested bean is currently in creation: Is there an unresolvable circular     reference?

A possible, but less than ideal solution would be to inject and EntityManager into UserRepositoryImp, but in that case, I do not have access to any of the Spring JPA methods provided by CrudRepository, or any additional methods that I might have created in UserRepository.

Any suggestions on how to get around this?

Any help would be greatly appreciated.

EDIT: As mentioned in @shelley's answer, I was able to solve this by making 3 changes:

SUCCESS!

解决方案

A couple small things need to be changed in order for this to work:

  • Remove the @Repository annotation from the custom repository interface (UserRepositoryExtension).

  • The custom repository implementation should actually be named "<StandardRepository>Impl" rather than "<CustomRepository>Impl". In your code example, this should be UserRepositoryImpl instead of UserRepositoryExtensionImpl.

这篇关于我的自定义存储库实现中的CrudRepository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆