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

240
Views
Método anotado Spring @Value, use el valor predeterminado cuando las propiedades no estén disponibles

Situación

Estoy inyectando propiedades del archivo .properties en campos anotados con @Value . Sin embargo, estas propiedades presentan credenciales confidenciales, por lo que las elimino del repositorio. Todavía quiero que, en caso de que alguien quiera ejecutar el proyecto y no tenga un archivo .properties con credenciales, los valores predeterminados se establecerán en los campos.

Problema

Incluso si configuro valores predeterminados para el campo en sí, obtengo una excepción cuando el archivo .properties no está presente:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'secret' in string value "${secret}"

Aquí está el campo anotado:

 @Value("${secret}") private String ldapSecret = "secret";

Esperaba que en este caso simplemente se estableciera String "secret".

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Para responder exactamente a tu pregunta...

 @Value("${secret:secret}") private String ldapSecret;

Y algunas variaciones más están a continuación para completar los ejemplos...

Por defecto una cadena a nulo:

 @Value("${secret:#{null}}") private String secret;

Por defecto un número:

 @Value("${someNumber:0}") private int someNumber;
about 4 years ago · Santiago Trujillo Report

0

Solo usa:

 @Value("${secret:default-secret-value}") private String ldapSecret;
about 4 years ago · Santiago Trujillo Report

0

@Value and Property Examples To set a default value for property placeholder : ${property:default value} Few examples : //@PropertySource("classpath:/config.properties}") //@Configuration @Value("${mongodb.url:127.0.0.1}") private String mongodbUrl; @Value("#{'${mongodb.url:172.0.0.1}'}") private String mongodbUrl; @Value("#{config['mongodb.url']?:'127.0.0.1'}") private String mongodbUrl;
about 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!