# How to load custom properties files in Spring Boot

**Date:** 2024-01-15

The Spring Framework, a powerful Java-based platform for building enterprise applications, offers a sophisticated mechanism for managing configuration settings.  Central to this mechanism is the `@PropertySource` annotation, a tool that allows developers to externalize application configuration, separating it from the core application code. This separation fosters cleaner, more maintainable, and easily adaptable applications.  The core principle is simple: instead of hardcoding settings directly into the Java code, these settings are stored in external files, typically text files with a `.properties` extension.  This approach allows for easy modification of settings without requiring recompilation of the application.

The `@PropertySource` annotation acts as a bridge between your application and these external configuration files.  It essentially tells the Spring framework where to look for these files, and which settings within the files to load into the application's environment. Think of it as a declarative instruction – you annotate a class, typically a configuration class, indicating the location of the properties files.  The framework then automatically reads the key-value pairs from these files and makes them available to the application.

Consider a scenario where you need to configure a database connection.  Instead of hardcoding the database URL, username, and password within your code, you would place these values in a properties file.  For example, a file named `database.properties` might contain:

`db.url=jdbc:mysql://localhost:3306/mydb`
`db.username=myuser`
`db.password=mypassword`

The `@PropertySource` annotation, applied to a configuration class, would then point to this `database.properties` file. This allows you to easily switch databases (e.g., to a test database) by simply modifying the `database.properties` file without altering a single line of your application's Java code.  This is particularly beneficial in scenarios involving different environments like development, testing, and production, each with its own unique configuration needs.

Spring Boot, a popular extension of the Spring Framework, simplifies the process even further. While Spring Boot utilizes `application.properties` or `application.yml` as default configuration files, it seamlessly integrates with `@PropertySource` to load additional custom files.  It's important to note that explicitly using `@PropertySource` for the default `application.properties` file is redundant, as Spring Boot automatically loads it. However, the annotation becomes crucial when you need to incorporate settings from other custom files.

Let's illustrate this with an example of a custom properties file named `custom.properties` containing:

`custom.message=This is a custom message`

To load this file, you would annotate a Spring configuration class with `@PropertySource("classpath:custom.properties")`. The `classpath:` prefix signifies that the file resides within the application's classpath – the set of directories and JAR files that the Java Virtual Machine (JVM) searches for resources.  Once this annotation is in place, Spring automatically loads the settings into the application environment.

You can then access these properties within your application using the `@Value` annotation.  For instance, if you had a bean (a managed object in Spring) with a field intended to hold the custom message, you'd annotate that field with `@Value("${custom.message}")`. This would inject the value "This is a custom message" into the bean's field. This eliminates the need for direct access to the properties file within your application logic.

The power of `@PropertySource` extends beyond single files.  The `@PropertySources` annotation provides a mechanism for loading multiple custom properties files. This annotation allows you to specify an array of `@PropertySource` annotations, each pointing to a different properties file. For instance, if you had `custom1.properties` and `custom2.properties`,  `@PropertySources` would allow you to load them both at once.  If there are conflicting keys between the files, the last loaded property file's value for that key takes precedence.

This ability to load multiple files is extremely useful for organizing settings logically.  You might have one properties file for database connections, another for email settings, and a third for application-specific parameters.  Each file would contain its own set of key-value pairs, promoting modularity and maintainability.  This structured approach makes configuration changes much easier to manage and less prone to errors.

In essence, the `@PropertySource` annotation and its companion `@PropertySources` represent best practices for configuration management in Spring-based applications.  By externalizing configuration settings, developers drastically reduce the complexity and enhance the flexibility of their applications.  It’s a key component of a well-structured, maintainable, and robust software architecture. This approach simplifies deployments across various environments, whether it's a simple development setup or a sophisticated multi-node production system.  The ability to modify settings without recompiling the application is a significant advantage in terms of development speed and operational efficiency.  The separation of configuration from core application logic is a cornerstone of modern software engineering principles, and `@PropertySource` elegantly facilitates this vital practice.


**[Read more](https://examples.javacodegeeks.com/how-to-load-custom-properties-files-in-spring-boot/)**
