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:
-
@Value Annotation:
You can use the
@Value
annotation to inject individual properties from theapplication.properties
orapplication.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 theapplication.properties
orapplication.yml
file will be injected into thecustomProperty
field. -
@ConfigurationProperties Annotation:
You can use the
@ConfigurationProperties
annotation to bind a set of properties from theapplication.properties
orapplication.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 theMyProperties
bean. -
@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 packagecom.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.