@Component
public class RandomA {
@Autowired
public RandomB randB;
public RandomA()
{
System.out.println("Bello");
}
public void get()
{
System.out.println("AAA");
}
}
@Component
public class RandomB {
public RandomB()
{
System.out.println("Hello");
}
@Autowired
public RandomA randA;
public void get()
{
System.out.println("BBB");
}
}
I was expecting the above code to give a circular dependency error in spring boot but it executes fine without any error.
On the other side:
@Component
public class RandomA {
public RandomB randB;
public RandomA(RandomB randB)
{
this.randB=randB;
System.out.println("Bello");
}
public void get()
{
System.out.println("AAA");
}
}
@Component
public class RandomB {
@Autowired
public RandomB(RandomA randA)
{
this.randA=randA;
System.out.println("Hello");
}
public RandomA randA;
public void get()
{
System.out.println("BBB");
}
}
This code leads to circular dependency error.
Can anyone explain why the 1st one don't lead to circular dependency?/
Thank you.
question from:https://stackoverflow.com/questions/66062428/field-based-and-constructor-based-circular-dependency-in-spring-boot