Home > Software engineering >  Why bean is circular autowiring?
Why bean is circular autowiring?

Time:09-15

I'm writting some REST API app and I faced with problem when bean autowires by himself. Idk what can cause this error because class UserRepositoryImpl doesn't have fields with his own datatype. Only interface and simple component bean

1st class

@Repository
    public class UserRepositoryImpl {

private final UserRepository repository;

private final UserDomainConverter converter;

@Autowired
public UserRepositoryImpl(UserRepository repository,
                          UserDomainConverter converter) {
    this.repository = repository;
    this.converter = converter;
}


//some methods
}

2nd class

@Component
public class UserDomainConverter {

//some methods
}

3rd class

    @Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {
    Optional<UserEntity> findByAuthToken(String authToken);

Optional<UserEntity> findByNickname(String nickname);

List<UserEntity> findUserEntitiesByIdIn(List<Long> idList);

List<UserEntity> findUserEntitiesByNicknameIn(String nickname);
}

OUTPUT

2022-09-14 23:56:06.379  INFO 21408 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-09-14 23:56:06.387  INFO 21408 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-09-14 23:56:06.403 ERROR 21408 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

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

   userController defined in file [D:\IdeaProjects\sigma-internship\target\classes\spotylike\spotylike\controller\UserController.class]
      ↓
   userService defined in file [D:\IdeaProjects\sigma-internship\target\classes\spotylike\spotylike\service\UserService.class]
┌─────┐
|  userRepositoryImpl defined in file [D:\IdeaProjects\sigma-internship\target\classes\spotylike\spotylike\repository\impl\UserRepositoryImpl.class]
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.


Process finished with exit code 1

CodePudding user response:

I believe you're stumbling across Spring Data JPA's repository implementation lookup mechanism. You can read a bit more in the javadoc but I think you'll get around it by renaming your class to something that doesn't end with Impl if you don't want to mess with the @EnableJpaRepositories-annotation.

CodePudding user response:

I thing you dont need to autowire the constructor of UserRepositoryImpl because the class has the reposotry-anotation

  • Related