-1

I am trying to load some properties from property files in Spring boot but also want to access them using a list. I am unsure about the order in which the class member will be initialised.

@Getter
@Setter
@ConfigurationProperties
public class ConfigurationM{

   @Value("${propA}")
   private String propA;

   @Value("${propB}")
   private String propB;

   private List<String> list = Collections.unmodifiableList(propA, propB);


}

Does @Value annotation injects value before the list is initialised? How is the order of evaluation determined when the class is loaded while using annotations?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Abhinav
  • 29
  • 1
  • 5

1 Answers1

0

Does @Value annotation injects value before the list is initialised?

Unfortunately, no! The "instance initializer"(class/object body) is executed prior to the "constructor", and spring has no chance to inject there anything.

How is the order of evaluation determined when the class is loaded while using annotations?

-> https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans


You could also evict the @PostConstruct with pure :

// pure (built-in) SpEL:
@Value("#{{'${propA}', '${propB}'}}")
private List<String> list;
    
// Java < 9
@Value("#{ T(java.util.Arrays).asList('${propA}', '${propB}')}")
private List<String> listJ8;
    
// Java > 8
@Value("#{ T(java.util.List).of('${propA}', '${propB}')}")
private List<String> listJ9;
xerx593
  • 12,237
  • 5
  • 33
  • 64