Injecting Properies in Spring boot


In this tutorial, we will find how to bind configuration properties to Structured Object.

Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. If these configurations are all related then we can bound these to java object using spring's @ConfigurationProperties annotation.


Ways to make configuration object injected into application

  • Annotate Application class with @EnableConfigurationProperties(value=MyProperties.class). In this case, there is no need to mark your configuration object with @Component annotation.
  • Or mark your configuration object with @Component annotation. This will make your class to be picked up by Spring's component scanner.

Find this project on Github

https://github.com/javacodenet/spring-boot-injecting-properties

pom.xml


Application.java


MyProperties.java

Reads properties from application.properties.

CustomProperties.java

Reads properties from custom.properties. We specified the location of the file as classpath along with its name.

application.properties


custom.properties


Output


Comments

Popular posts from this blog

Generate Random values in Spring boot

Generate PDF From XML Or Java Object Using Apache FOP

Generate/Read an Excel file in Java using Apache POI