spring

How to load custom properties files in Spring Boot?

The @PropertySource annotation in Java is a component of the Spring Framework, specifically used for externalizing configuration. Employed on a class, it denotes the source of property values to be injected into the Spring Environment. With this annotation, developers can specify the location of property files containing key-value pairs, allowing easy customization of application settings without altering code. This promotes a clean separation of configuration from code, enhancing flexibility and maintainability in Spring-based applications. Let us delve into understanding how to load custom properties files in Spring boot.

1. @PropertySource Annotation in Java

The @PropertySource annotation is a crucial component in the Spring Framework, designed for efficient configuration management. When applied to a class, it signals the source of property values to be injected into the Spring Environment. This annotation enables developers to specify the location of property files containing key-value pairs, facilitating the customization of application settings without the need to modify the underlying code. The primary goal is to maintain a clean separation between configuration and code, fostering flexibility and ease of maintenance in Spring-based applications.

To use @PropertySource, developers typically annotate a configuration class with the location of the property file. This file holds configurations such as database URLs, connection details, or any other parameter that may vary between environments. By leveraging this annotation, developers can alter application behavior without recompiling the code, which is particularly advantageous in diverse deployment scenarios.

The annotation exemplifies a best practice in the Spring ecosystem, encouraging modular and extensible design by externalizing configuration settings. This promotes a more robust and adaptable architecture, as managing, updating, and deploying applications across various environments becomes easier.

2. Load default properties file

Consider a properties file named application.properties with the following content:

# application.properties

database.url=jdbc:mysql://localhost:3306/mydatabase
database.username=myuser
database.password=mypassword

Now, in your Spring configuration class, you can employ the @PropertySource annotation to load this default properties file. However, since application.properties or application.yml serves as the default resource configuration file for the Spring framework, there is no need to use the @PropertySource annotation with the resource configuration file. It’s important to note that if the application.properties or application.yml file is present in a custom location, then in this case, the @PropertySource will be utilized with a custom classpath.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
public class AppConfig {
    // Your configuration code goes here
}

In this example, you can use the @Value annotation to inject the properties into your beans from the properties file.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    @Value("${database.url}")
    private String databaseUrl;

    @Value("${database.username}")
    private String databaseUsername;

    @Value("${database.password}")
    private String databasePassword;

    // Rest of your component code
}

Now, the properties of the application.properties file will be injected into the corresponding fields of MyComponent.

3. Load custom properties files

In addition to loading default properties files, Spring allows you to load custom properties files. You can use the @PropertySource annotation with the value attribute specifying the path to your custom properties file. Here’s an example.

Consider a custom properties file named custom.properties with the following content:

# custom.properties

custom.property=value123

Now, you can load this custom properties file in your Spring configuration class as follows:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:custom.properties")
public class CustomConfig {
    // Your configuration code goes here
}

In this example, @PropertySource("classpath:custom.properties") specifies that the custom properties file is located on the classpath. Again, adjust the file path accordingly if it’s in a different location. After adding this annotation to your configuration class, you can use the @Value annotation to inject the properties from the custom file into your beans:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyCustomComponent {

    @Value("${custom.property}")
    private String customProperty;

    // Rest of your component code
}

Now, the property custom.property from the custom.properties file will be injected into the MyCustomComponent. Make sure to adjust the key (custom.property) according to your custom properties file.

4. Load multiple custom properties files

To load multiple custom properties files in a Spring application, you can use the @PropertySources annotation with an array of @PropertySource annotations. Each @PropertySource annotation within the array represents a different properties file. Here’s an example.

Consider you have two custom properties files named custom1.properties and custom2.properties with the following contents:

# custom1.properties
custom.property1=value123

# custom2.properties
custom.property2=value456

Now, you can load these custom properties files in your Spring configuration class:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({
    @PropertySource("classpath:custom1.properties"),
    @PropertySource("classpath:custom2.properties")
})
public class MultipleCustomConfig {
    // Your configuration code goes here
}

In this example, the @PropertySources annotation is used to specify an array of @PropertySource annotations, each pointing to a different custom properties file. Adjust the file paths accordingly based on your project structure. After adding this annotation to your configuration class, you can use the @Value annotation to inject the properties from these custom files into your beans:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyMultipleCustomComponent {

    @Value("${custom.property1}")
    private String customProperty1;

    @Value("${custom.property2}")
    private String customProperty2;

    // Rest of your component code
}

Now, both custom.property1 and custom.property2 from custom1.properties and custom2.properties will be injected into the MyMultipleCustomComponent. Adjust the keys accordingly based on your custom properties files.

5. Conclusion

In conclusion, the @PropertySource annotation in the Spring Framework is a pivotal tool for externalizing configuration in Java applications. By employing this annotation, developers can seamlessly load properties from external files, promoting a clean separation between configuration settings and code. Whether it’s loading default properties or multiple custom properties files, the flexibility offered by @PropertySource enhances the adaptability of Spring-based applications. This approach not only simplifies the management of diverse configurations but also facilitates the customization of application settings without the need for code modifications. Embracing this annotation aligns with best practices in Spring development, fostering modular, maintainable, and easily deployable applications. As we navigate the landscape of modern software development, the @PropertySource annotation remains an invaluable asset for achieving configurational elegance and flexibility within the Spring ecosystem.

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button