Click here to Skip to main content
16,022,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Keep on getting 'Error creating bean with name 'xxxx': Requested bean is currently in creation: Is there an unresolvable circular reference?' when I am trying to use @Autowired annotation in the Java spring boot automation project.

I have two different classes (lets say class A and class B) within the project and I want to use @Autowired annotation to call the methods from one class to another. I do not see any issue if I create an instance of an object of the class A in class B but when I use @Autowired then it is throwing an applicationcontext error. I am new to the spring boot concepts and want some help to fix this code. Appreciate your help.


What I have tried:

@Component
public class Vehicle implements BasePage {

  @Autowired //This is working good
  private Car cars;

  public void method1() {}

  public void method2() {}

  public void method3(){
    cars.methodDrive();
  }
}


@Component
public class Car {

 @Autowired //not working and throwing applicationcontext error
 private Vehicle vehicle

 //private Vehicle vehicle=new Vehicle(); //this is working good

 public void methodCars(){
   vehicle.method1();
   vechicle.method2();
 }

 public void methodDrive(){}



My springboot runner class
@RunWith(SpringRunner.class)
@CucumberContextConfiguration
@SpringBootTest
public class SpringBootRunner {

    @Before
    public void setupCucumberSpringContext() {

    }
}
Posted
Updated 1-Aug-24 7:45am
v4

1 solution

The circular reference you are seeing in the sample you have posted is because you have autowired up Car in your vehicle class, and also autowired Vehicle in your car class. While Spring does have a property to ignore circular dependencies, you might want to think about whether you actually want to do this. To be honest, it looks like you are mixing up inheritance and IoC here. The override in the application.yml file would look like:
YAML
spring:
  main:
    allow-circular-references: true
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900