• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Why I can't extract server's port value from application.yml file?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have application.yml file with following properties:

When I run my app, it shows application is running on port 8050.

And then I wanted to "extract" that value using some @Configuration annotated class:

What I get is:

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?
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Stefan Jankovic
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the very good reply. Everything is clear now
 
Rob Spoor
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic