Stefan Jankovic wrote:Can someone explain this weird behavior? I have done reading from yml files several times and it worked every single time. Well, except with this case. What am I missing?
I think you're running into an order issue. The printing is done in an initializer block. That runs after any call to a parent constructor (
super(...) inside a constructor) but before any constructor body. After the constructor is done (in your case, you only have a default one) your object is initialized. It's only then that Spring can perform its magic and inject the values for these fields. That means that the injection is done after the printing.
Spring provides some techniques that allow you to inject these fields in a way you want. I can think of the following:
1) Constructor injection. Spring will call a constructor with the injected values if you annotate the constructor arguments with
@Value.
2) Any method annotated with
@PostConstruct is called after any injection is performed.
3) Implement
InitializingBean. Its
afterPropertiesSet method is called after the properties are injected (duh).
Note that you can probably also inject
ServerProperties if you need more options. It still needs to be injected before you can access it like the separate properties.