thombergs / code-examples

A collection of code examples from blog posts etc.
MIT License
2.65k stars 2.63k forks source link

Critical information missing in your Properties example #300

Open RobertBrown3 opened 8 months ago

RobertBrown3 commented 8 months ago

I have looked with great interest at your @ConfigurationProperties article and code:

https://reflectoring.io/spring-boot-configuration-properties/ and spring-boot/configuration/src/main/java/io/reflectoring/configuration on GitHub

because I am working on utilizing it for the creation of custom properties on my Spring Boot application. Unfortunately, while the information you gave was pretty complete on how to use @ConfiguratioProperties to create configuration properties, there is one pretty major piece of information that is missing:

How do you access the properties once you have defined them and made Spring boot "aware" of them???

Using your properties as an example: you made the MailModuleProperties class to add the twp properties "enabled" and "defaultSubject", and you created two versions of the MailModuleConfiguration class to make a MailModuleProperties instance available to the Spring Boot application. Well and good, but how do you actually access and use the two properties once you make them together?

Suppose I want to create a Controller class with a @GetMapping annotated method that wraps the value of the "defaultSubject" property in some HTML and returns it to a client application or browser? How do I access the MailModuleProperties instance in order to get the property???

I did try to use the @Value annotation to set a string to the value of defaultSubject for use in a Controller method, but the string was null when I attempted to use it.

Without that critical piece of information, your information about using @ConfigurationProperties is pretty unusable.

Can you please include an example of how one actually accesses and uses the properties you made in a Controller or Service class?