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

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

问题描述

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

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.

原油扩展:

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

扩展接口:

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

自定义实现:

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;
    }...
}

但是,我无法注入 UserRepository,因为存在循环依赖(假设 UserRepository 扩展了由我的 UserRepositoryImpl 实现的接口).我收到以下错误:

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?

一种可能但不太理想的解决方案是将 EntityManager 注入 UserRepositoryImp,但在这种情况下,我无法访问任何 Spring JPACrudRepository 提供的方法,或我可能在 UserRepository 中创建的任何其他方法.

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?

任何帮助将不胜感激.

正如@shelley 的回答中所述,我能够通过进行 3 次更改来解决此问题:

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

  • Removing the @Repository from UserRepositoryExtensionImpl
  • Renaming UserRepositoryExtensionImpl to UserRepositoryImpl. Apparently this makes Spring aware of the implementation's existence. See Spring Doc
  • Removing my constructor and moving the @Autowired to the userRepository field

成功!

推荐答案

一些小事情需要改变才能使其发挥作用:

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

  • 从自定义存储库界面 (UserRepositoryExtension) 中删除 @Repository 注释.

自定义存储库实现实际上应该命名为Impl"而不是Impl".在您的代码示例中,这应该是 UserRepositoryImpl 而不是 UserRepositoryExtensionImpl.

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天全站免登陆