Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda