1

I have a spring boot application, in that my @Autowire is not working in some situations.

@RestController
public class Check {

@Autowired
SettlementFinanceRepo sfr;

@RequestMapping("/api/checker")
public String checkSettlementFinance(String id) {
    SettlementFinanceEntity sfe=new SettlementFinanceEntity();
    String status="success";
    try {
        sfe.setTransaction_id(id);
        sfr.save(sfe);
    }catch(Exception e) {
        e.printStackTrace();
        status=e.getMessage();
    }
    return status;
}
}

The above code is working completely fine, when i call it the REST way http://localhost:8080/api/checker?id=99999 my autowired repo can able to save it into the database.

But when i call this method, from another @RestController class, @Autowired SettlementFinanceRepo sfr getting null

Example:

@RestController
public class SomeRestClass{

Check c=new Check();

public void letsSave(){
c.checkSettlementFinance("99999");
}

}

In this case, my @Autowired SettlementFinanceRepo sfr from Check class is getting null, can someone explain what im missing here...

Thanks!

1 Answers1

0

You are calling a restcontroller class from another restcontroller class. This might be causing the issue, you should use a service and call the service class from the controller.

Also you separate out the logic by following DAO, you can find more about DAO here : https://www.baeldung.com/jsf-spring-boot-controller-service-dao

If this does not help then please comment we can discuss and find a solution.