Spring Boot @PropertySource Example

1. Introduction

Spring Boot's @PropertySource annotation allows you to specify the location of property files that should be loaded into the Spring Environment. This is particularly useful for separating configuration from code and for maintaining environment-specific properties.

Key Points:

1. @PropertySource is used to declare a set of properties defined in a file within Spring's Environment.

2. It is typically used in conjunction with @Configuration classes.

3. Property sources are loaded in the order they are declared and can override properties defined in previously loaded files.

2. Implementation Steps

1. Create an external properties file with some configuration properties.

2. Create a @Configuration class and use the @PropertySource annotation to load the properties file.

3. Use @Value to inject the property values into beans.

4. Run the Spring Boot application and observe the injected values.

3. Implementation Example

Here is the complete code that demonstrates the usage of @PropertySource annotation:
// Step 1: Create an external properties file named app-config.properties
# src/main/resources/app-config.properties
external.message=Hello from external config

// Step 2: Create a configuration class to load the external properties file
@Configuration
@PropertySource("classpath:app-config.properties")
public class AppConfig {
    // This class will be responsible for loading the properties file
}

// Step 3: Create a component that uses the @Value annotation to inject property values
@Component
public class ExternalConfigComponent {

    @Value("${external.message}")
    private String message;

    public String getMessage() {
        return message;
    }
}

// Step 4: Create the main application class
@SpringBootApplication
public class PropertySourceExampleApplication implements CommandLineRunner {

    @Autowired
    private ExternalConfigComponent externalConfigComponent;

    public static void main(String[] args) {
        SpringApplication.run(PropertySourceExampleApplication.class, args);
    }

    @Override
    public void run(String... args) {
        // Print the injected message from the external configuration
        System.out.println(externalConfigComponent.getMessage());
    }
}

Output:

Hello from external config

Explanation:

1. @PropertySource("classpath:app-config.properties") in the AppConfig class specifies the location of the properties file to be loaded.

2. app-config.properties contains a custom property external.message which holds the string that will be injected.

3. @Value("${external.message}") in ExternalConfigComponent injects the value of external.message into the message field.

4. PropertySourceExampleApplication, annotated with @SpringBootApplication, serves as the bootstrap class for the application.

5. The run method from the CommandLineRunner interface in PropertySourceExampleApplication executes after the application context is started, accessing the getMessage method from ExternalConfigComponent.

6. The output "Hello from external config" is the result of reading the property from app-config.properties and injecting it into the ExternalConfigComponent using the @PropertySource and @Value annotations.


Comments