Reading from Properties Files in Spring Boot

January 1, 2020

I wanted to read some settings from a properties file and have it available to my Spring Boot application. This can be done in the following way:

package com.drumcoder.diary.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = { "classpath:application.properties" })
public class AzureActiveDirectoryConfig {
    @Value("${spring.security.oauth2.client.registration.azure.client-id}")
    public String azureClientId;

    @Value("${spring.security.oauth2.client.registration.azure.client-secret}")
    public String azureClientSecret;

    @Value("${azure.activedirectory.tenant-id}")
    public String azureTenantId;
}

where the application.properties file in src/main/resources contains

# Specifies your Active Directory ID:
azure.activedirectory.tenant-id=xxxxxx-xxxx-xxxx-xxxxxx

# Specifies your App Registration's Application ID:
spring.security.oauth2.client.registration.azure.client-id=xxxxxx-xxxx-xxxx-xxxxxx

# Specifies your App Registration's secret key:
spring.security.oauth2.client.registration.azure.client-secret=xxxxxx-xxxx-xxxx-xxxxxx