1

I am using autowired annotation for the dependency injection in my app. How can I avoid cyclic dependency while using constructor injection.

public interface CustomUserRepository {
 void myCustom();
}

public interface UserRepository extends CustomUserRepository {
 ----
}

public class UserRepositoryImpl implements CustomUserRepository {

@Autowired UserRepository self;  //Here I have the Cyclic dependency

}
6
  • Show some code and explain in detail what you want and what not. Commented Nov 17, 2017 at 11:19
  • Elaborate what you have tried sofar to improve quality of your question. Commented Nov 17, 2017 at 11:20
  • Please extend your question by providing example code. In any case, however, you should not have cyclic dependencies as they indicate bad design and mixed responsibilites Commented Nov 17, 2017 at 11:21
  • Try googling it first: softwareengineering.stackexchange.com/questions/253646/… Commented Nov 17, 2017 at 11:29
  • And this: baeldung.com/circular-dependencies-in-spring Commented Nov 17, 2017 at 11:30

1 Answer 1

1

Spring does not support cyclic dependencies with constructor injection. The rationale is that an object must be fully initialized before it can be injected.

To resolve the issue make your circular dependency into a property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.