Ghost Rider
Ghost Rider

Reputation: 161

how to avoid cyclic dependency whle using constructor injection

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

}

Upvotes: 1

Views: 524

Answers (1)

Jasper Blues
Jasper Blues

Reputation: 28756

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.

Upvotes: 1

Related Questions