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

326
Views
Spring @Value lista vacía por defecto

¿Hay alguna manera de establecer una lista vacía como valor predeterminado para una propiedad en Spring, algo así como:

 @Value("${my.list.of.strings :" + new ArrayList<>() + "}") private List<String> myList;

Obviamente, no es una nueva ArrayList, pero necesito una lista vacía allí.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Después de echar un vistazo a la especificación SpEL y combinarla con la respuesta de @javaguy, se me ocurrió esto:

 @Value("${my.list.of.strings:}#{T(java.util.Collections).emptyList()}") private List<String> myList;
about 4 years ago · Santiago Trujillo Report

0

En realidad, en las versiones actuales de Spring solo funciona un símbolo : con un valor empty default .

El ejemplo completo que estoy usando:

 @Value("${my.custom.prop.array:}") private List<Integer> myList;

Para estar seguro y más seguro, también agrego init a la variable List:

 @Value("${my.custom.prop.array:}") private List<Integer> myList = new ArrayList<>();
about 4 years ago · Santiago Trujillo Report

0

@Value("#{T(java.util.Arrays).asList('${my.list.of.strings:}')}") private List<String> myList;

funciona para mí, usando Spring 5.0.x (da una lista vacía, si su propiedad my.list.of.strings no está configurada en contexto)

después puedes hacer fácilmente algo como

 CollectionUtils.isNotEmpty(myList)
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!