Return to site

🍃🎓 SPRING CERTIFICATION QUESTION: How are properties defined? Where is Spring Boot’s default property source?

February 14, 2023

Answer:

Spring Boot application allows specifying properties in lots of ways.

They are evaluated in the following order (excerpt from documentation):

  • 1. Devtools global settings properties on your home directory (~/.https://lnkd.in/e5CRZep2 when devtools is active).
  • 2. {@link TestPropertySource} annotations on your tests.
  • 3. {@link SpringBootTest#properties()} annotation attribute on your tests.
  • 4. Command line arguments.
  • 5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  • 6. {@link ServletConfig} init parameters.
  • 7. {@link ServletContext} init parameters.
  • 8. JNDI attributes from java:comp/env.
  • 9. Java System properties (System.getProperties()).
  • 10. OS environment variables.
  • 11. A {@link RandomValuePropertySource} that has properties only in random.*.
  • 12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  • 13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  • 14. Application properties outside of your packaged jar (application.properties and YAML variants).
  • 15. Application properties packaged inside your jar (application.properties and YAML variants).
  • 16. {@link PropertySource} annotations on your {@link Configuration} classes.
  • 17. Default properties (specified by setting {@link SpringApplication#setDefaultProperties}).

Spring Boot's default property source is an application.properties file.

Note: Spring Boot offers an additional way to bind configuration properties to {@link Bean bean-methods} or simple Java classes.

{@link ConfigurationProperties} annotation doesn't support SpEL features but allows to validate injected values using JSR-303 compliant implementation.

#spring #certificationquestion #vcp