问题描述
我试图在我的自定义实现中获取对我的存储库接口(提供的任何Spring JPA方法。 code>,或者我可能在UserRepository中创建的任何其他方法。
有关如何解决此问题的任何建议?
任何帮助将不胜感激。编辑:正如@ shelley的回答中提到的,我能够通过进行3次更改来解决这个问题:
/ p>
- 从 UserRepositoryExtensionImpl $ c $ <$>重命名 UserRepositoryExtensionImpl 至 UserRepositoryImpl 。显然这让Spring意识到了实现的存在。请参阅 Spring Doc a>
- 删除我的构造函数并将 @Autowired 移动到 userRepository
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:
- 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
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!