I am getting below error message when I am running my spring boot application.
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfiguration (field private com.prity.springbootdemo1.service.UserService com.prity.springbootdemo1.config.SecurityConfiguration.userService)
↑ ↓
| userServiceImpl (field private org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder com.prity.springbootdemo1.service.UserServiceImpl.passwordEncoder)
└─────┘
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.
You can try with this. Add it to file application.properties
spring.main.allow-circular-references=true
And try to run. This is not best solution you still need to find better way to fix problem.
So if you have a class A
and you somehow run into this problem when injecting the class A
into the constructor of class B
, use the @Lazy
annotation in the constructor of class B
. This will break the cycle and inject the bean of A
lazily into B
. So, instead of fully initializing the bean, it will create a proxy to inject into the other bean. The injected bean will only be fully created when it's first needed.
@Component
public class CircularDependencyA {
private CircularDependencyB circB;
@Autowired
public CircularDependencyA(@Lazy CircularDependencyB circB) {
this.circB = circB;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With