The dependencies of some of the beans in the application context form a loop

I am working on a Spring Boot application v1.4.2.RELEASE with JPA.

I defined interfaces and repository implementations

ARepository

@Repository public interface ARepository extends CrudRepository<A, String>, ARepositoryCustom, JpaSpecificationExecutor<A> { } 

ARepositoryCustom

 @Repository public interface ARepositoryCustom { Page<A> findA(findAForm form, Pageable pageable); } 

ARepositoryImpl

 @Repository public class ARepositoryImpl implements ARepositoryCustom { @Autowired private ARepository aRepository; @Override public Page<A> findA(findAForm form, Pageable pageable) { return aRepository.findAll( where(ASpecs.codeLike(form.getCode())) .and(ASpecs.labelLike(form.getLabel())) .and(ASpecs.isActive()), pageable); } } 

And service AServiceImpl

 @Service public class AServiceImpl implements AService { private ARepository aRepository; public AServiceImpl(ARepository aRepository) { super(); this.aRepository = aRepository; } ... } 

My application will not start with a message:

  ****************************
 APPLICATION FAILED TO START
 ****************************

 Description:

 The dependencies of some of the beans in the application context form a cycle:

 |  aRepositoryImpl
 └─────┘

I followed all the steps described in http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour

Please, help!

Laurent

+5
source share
2 answers

There is a simple fix for your original problem: Just remove @Repository from ARepositoryCustom and from ARepositoryImpl. Keep all hierarchies of names and interfaces / classes. Everything is good.

+4
source

I tested your source code and found something complicated.

Firstly, with the source code, I got the following error:

 There is a circular dependency between 1 beans in the application context: - ARepositoryImpl (field private test.ARepository test.ARepositoryImpl.aRepository) - aRepositoryImpl 

Then, I think Spring is 'confused' between ARepository (JPA repository) and ARepositoryImpl (user repository). So, I suggest you rename ARepository to something else, like BRepository . It worked if I renamed the class name.

According to the official Spring Data documentation ( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ ):

These classes must follow the naming convention by adding the repository-impl-postfix attribute of the namespace attributes to the interface name of the found repository. This postfix defaults to Impl.

+1
source

Source: https://habr.com/ru/post/1262681/


All Articles