Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
Autowired instance is null using constructor

New to Springboot I know this has been asked so many times....

I have the following controller which has a CustomRestTemplate autowired. CustomRestTemplate has another bean autowired called RestClientConfig .

RestClientConfig is always null? I annotated with @Component but it never gets initialized inside CustomRestTemplate

@RestController
@RequestMapping("/catalog")
public class CatalogController {

  @Autowired
  private CustomRestTemplate restTemplate;
.
.

CustomRestTemplate has RestClientConfig autowired

RestClientConfig injected inside CustomRestTemplate is always null, why is that?

@Component
public class CustomRestTemplate{

  @Autowired
  private RestClientConfig restClientConfig // always null;

  public CustomRestTemplate()
  {
    
  }
}
@Component
public class RestClientConfig {

  private String val;

  public RestClientConfig()
  {
    this.val = "test";
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It is because of the no-arg constructor provided in CustomRestTemplate class which takes precedence while creating spring bean, there are two ways to fix this remove the constructor and Autowire using field

@Component
public class CustomRestTemplate{

   @Autowired
   private RestClientConfig restClientConfig;


}

Or add RestClientConfig as an argument in the constructor and use constructor autowiring

@Component
public class CustomRestTemplate{

  
  private RestClientConfig restClientConfig;

  @Autowired
  public CustomRestTemplate(RestClientConfig restClientConfig) {
    this.restClientConfig = restClientConfig;
    }
 }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!