@@ -5243,6 +5243,199 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
`ApplicationContext`.
[[beans-value-annotations]]
=== Using `@Value`
You can use `@Value` annotation to inject a default value to a constructor parameter, as the following example shows:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class MovieRecommender {
private final String algorithm;
public MovieRecommender(@Value("myRecommendationAlgorithm") String algorithm){
this.algorithm = algorithm;
}
}
----
You can also apply the `@Value` annotation to fields, as the following example shows:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class MovieRecommender {
@Value("myAlgorithm")
private String algorithm;
}
----
Built-in converter support provided by Spring allows simple type conversion to be automatically handled, as the following example shows:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class MovieRecommender {
private final Integer depth;
public MovieRecommender(@Value("2") Integer depth){
this.depth = depth;
}
}
----
As the annotation java type does not allow non constant value such as null, you can also safely use primitive type parameter:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class MovieRecommender {
private final int depth;
public MovieRecommender(@Value("2") int depth){
this.depth = depth;
}
}
----
Multiple comma separated values parameter can be automatically converted to String array without extra effort:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class MovieRecommender {
private final String[] catalogs;
public MovieRecommender(@Value("catalogA,catalogB") String[] catalogs){
this.catalogs = catalogs;
}
}
----
Spring BeanPostProcessor uses ConversionService instance behind the scene to handle the process from converting
`@Value` value to the target type.
If you want to provide conversion support for your own custom type, you can provide your own ConversionService bean instance as the following example shows:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@Configuration
public class MyConfig {
@Bean
public static ConversionService conversionService() {
DefaultFormattingConversionService defaultFormattingConversionService = new DefaultFormattingConversionService();
public PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
----
Using the above configuration ensures Spring initialization failure if any of "${}" placeholder could not be resolved.
NOTE: Spring Boot configures by default a `PropertySourcesPlaceholderConfigurer` bean that will get properties from `application.properties` and `application.yml` files.