Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda