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:
@Repository
from UserRepositoryExtensionImpl
UserRepositoryExtensionImpl
to UserRepositoryImpl
. Apparently this makes Spring aware of the implementation's existence. See Spring Doc
@Autowired
to the userRepository
fieldSUCCESS!
JPA repository extends CrudRepository and PagingAndSorting repository. It inherits some finders from crud repository such as findOne, gets and removes an entity.
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
.
As shelley pointed out, the naming is really important to make the autowire work. In the example below, I follow the right naming standard for my custom interface and its implementation. But my interface that extended the JpaRepository was named “ItemDao” instead of “ItemRepository”, this resulted in that spring ignored my custom implementation altogether...
OBS!!! Should be "ItemRepository"
@Repository
public interface ItemDao extends JpaRepository<Item, Long>, ItemRepositoryCustom {}
my interface
interface ItemRepositoryCustom {...}
my implementation class
class ItemRepositoryImpl implements ItemRepositoryCustom {...}
If anyone have similar problems, start by following the naming standard that is used in the spring documentation at the link below.
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With