• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 42
  • 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?
 
Marshal
Posts: 22409
121
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: 42
  • 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
Marshal
Posts: 22409
121
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic