How do you inject custom application properties?

ghz 9months ago ⋅ 56 views

To inject custom application properties into your Spring Boot application, you can use Spring's @Value annotation, @ConfigurationProperties annotation, or Spring Boot's @ConfigurationPropertiesScan annotation. Here's how you can use each approach:

  1. @Value Annotation:

    You can use the @Value annotation to inject individual properties from the application.properties or application.yml file into your Spring components:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyComponent {
    
        @Value("${custom.property.name}")
        private String customProperty;
    
        public void doSomething() {
            System.out.println("Custom property value: " + customProperty);
        }
    }
    

    In this example, the custom.property.name property from the application.properties or application.yml file will be injected into the customProperty field.

  2. @ConfigurationProperties Annotation:

    You can use the @ConfigurationProperties annotation to bind a set of properties from the application.properties or application.yml file to a Java bean:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "custom")
    public class MyProperties {
    
        private String propertyName;
    
        public String getPropertyName() {
            return propertyName;
        }
    
        public void setPropertyName(String propertyName) {
            this.propertyName = propertyName;
        }
    }
    

    In this example, properties with the prefix custom (e.g., custom.property-name) will be bound to the MyProperties bean.

  3. @ConfigurationPropertiesScan Annotation:

    You can use the @ConfigurationPropertiesScan annotation to automatically register beans annotated with @ConfigurationProperties in your application context:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "custom")
    public class MyProperties {
    
        private String propertyName;
    
        public String getPropertyName() {
            return propertyName;
        }
    
        public void setPropertyName(String propertyName) {
            this.propertyName = propertyName;
        }
    }
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
    
    @SpringBootApplication
    @ConfigurationPropertiesScan("com.example")
    public class MyApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    In this example, all components annotated with @ConfigurationProperties in the package com.example will be registered as beans in the application context.

Choose the approach that best fits your application's requirements and preferences. The @Value annotation is suitable for injecting individual properties, while @ConfigurationProperties is more appropriate for binding groups of related properties to Java beans. The @ConfigurationPropertiesScan annotation is useful for automatically registering beans with @ConfigurationProperties annotations.