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
fromUserRepositoryExtensionImpl
- Renaming
UserRepositoryExtensionImpl
toUserRepositoryImpl
. Apparently this makes Spring aware of the implementation's existence. See Spring Doc - Removing my constructor and moving the
@Autowired
to theuserRepository
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 beUserRepositoryImpl
instead ofUserRepositoryExtensionImpl
.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"
my interface
my implementation class
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
I have solved problem by injecting
ApplicationContext
and getting bean in lazy way usingapplicationContext.getBean(UserRepository.class)
. It works this way.I found I way of how to do it without the need for
@Autowire
:Well in this case I suggest to use the
@Lazy
annotation.With constructor parameter Spring tries to create the "basic" repository class which require you custom repository which requires you "basic" repository - the typical case with circular dependency.
Without
@Lazy
but with only the@Autowired
it also won't work (there will be problem with factory bean for the basic repo).I think in this case the
@Lazy
is the most elegant solution.There is a well defined way to create custom repository implementations in Spring Data JPA which you should follow. Basically you need to extend
CrudRepository
so you don't have to inject an instance of it in your custom implementation.