Springboot all possible ways to read props from Configuration file.

Reading Time: 2 minutes
Exploring Spring Boot :Beginners Level - Knoldus Blogs

Springboot YML/ Properties file

Various properties can be specified inside your application.properties file or inside your application.yml file, the following ways to reading properties from YML or Properties file.

  • Reading a single property
  • Read bunch of properties then use can use @ConfigurationProperties.
  • Read entire YML/Property file

Visit following URL and here you can find more properties:
https://docs.spring.io/spring-boot/docs/1.1.6.RELEASE/reference/html/common-application-properties.html

https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html

In my case i’m using YML file

voyagers:
  enabled: true
  basePath: "/"
  mapping: "/voyager"
  endpoint: "/graphql"

Reading a single property

You can use @Value(“${<property-name>}) annotation
@Value("${voyagers.enabled}")
private String voyagerEnable;

Read bunch of properties

If you want to read bunch of properties then there are two ways:

  • @ConfigurationProperty(prefix) with @EnableConfigurationProperties annoation
  • @ConfigurationProperty(prefix) with @Components
@ConfigurationProperty(prefix) with @EnableConfigurationProperties(<property class>) annoation

Let’s register our configuration bean using @EnableConfigurationProperties:

@EnableConfigurationProperties – Creates a binding between a configuration entity class and Spring configuration stereotype so that after injection within a service properties can be retrieved easily.


@ConfigurationProperties(prefix = "voyagers")
public class AppProperties {
private Boolean enabled;    private String basePath;
    private String mapping;
    private String endpoint;
}@Service
public class AppPropertiesDemo {
    @Autowired
    private AppProperties appProperties;

    void checkProp(){
        System.out.println("Reading bunch of app properties =>" + appProperties);
    }
}

Main class:
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class GraphQlApplication implements ApplicationRunner {
	@Autowired
	private AppPropertiesDemo appPropertiesDemo;
	public static void main(String[] args) {
		SpringApplication.run(GraphQlApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		appPropertiesDemo.checkProp();
	}
}
@ConfigurationProperty(prefix) with @Components

@ConfigurationProperties – Used to bind a class with an externalized property file. Very powerful and must be used to separate out bean classes with configuration entity class.

@ConfigurationProperties(prefix = "voyagers")
@Component
public class AppProperties {
    private Boolean enabled;
    private String basePath;
    private String mapping;
    private String endpoint;
}

We can autowire this property class as like that:
@Autowired
private AppProperties appProperties;

Read entire yml or property file

Spring Boot initializes its environment, it uses properties files, YAML files, environment variables, and command-line arguments to externalize the configuration.

This section i will discuss how to read the application property but it’s not a recommended approach, instead of that we use @Value annotation..

For that we can autowire spring framework class Environment bean. The @SpringBootApplication annotation enables auto-configuration and component scanning.

@Autowired
private Environment env;

log.info("Reading single property"+ environment.getProperty("voyagers.enabled"));

In this blog we have covered various way (Single | Multiple Properties | Entire Properties) to read properties of spring/spring-boot configuration file.

Written by 

Abid Khan is a Lead Consultant at Knoldus Inc., postgraduate (MCA), and having 5+ years of experience in JavaSE, JavaEE, ORM framework, Spring, Spring-boot, RESTful Web Services, Kafka, MQTT, Rabbitmq, Docker, Redis, MySQL, Maven, GIT, etc. He is a well-developed professional with a prolific track record of designing, testing, and monitoring software as well as upgrading the existing programs.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading