You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/docs/asciidoc/core/core-beans.adoc
+193
Original file line number
Diff line number
Diff line change
@@ -5243,6 +5243,199 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
5243
5243
`ApplicationContext`.
5244
5244
5245
5245
5246
+
[[beans-value-annotations]]
5247
+
=== Using `@Value`
5248
+
5249
+
You can use `@Value` annotation to inject a default value to a constructor parameter, as the following example shows:
5250
+
5251
+
[source,java,indent=0]
5252
+
[subs="verbatim,quotes"]
5253
+
----
5254
+
public class MovieRecommender {
5255
+
5256
+
private final String algorithm;
5257
+
5258
+
public MovieRecommender(@Value("myRecommendationAlgorithm") String algorithm){
5259
+
this.algorithm = algorithm;
5260
+
}
5261
+
}
5262
+
----
5263
+
5264
+
You can also apply the `@Value` annotation to fields, as the following example shows:
5265
+
5266
+
[source,java,indent=0]
5267
+
[subs="verbatim,quotes"]
5268
+
----
5269
+
public class MovieRecommender {
5270
+
5271
+
@Value("myAlgorithm")
5272
+
private String algorithm;
5273
+
}
5274
+
----
5275
+
5276
+
5277
+
Built-in converter support provided by Spring allows simple type conversion to be automatically handled, as the following example shows:
5278
+
5279
+
5280
+
[source,java,indent=0]
5281
+
[subs="verbatim,quotes"]
5282
+
----
5283
+
public class MovieRecommender {
5284
+
5285
+
private final Integer depth;
5286
+
5287
+
public MovieRecommender(@Value("2") Integer depth){
5288
+
this.depth = depth;
5289
+
}
5290
+
}
5291
+
----
5292
+
5293
+
As the annotation java type does not allow non constant value such as null, you can also safely use primitive type parameter:
5294
+
5295
+
[source,java,indent=0]
5296
+
[subs="verbatim,quotes"]
5297
+
----
5298
+
public class MovieRecommender {
5299
+
5300
+
private final int depth;
5301
+
5302
+
public MovieRecommender(@Value("2") int depth){
5303
+
this.depth = depth;
5304
+
}
5305
+
}
5306
+
----
5307
+
5308
+
Multiple comma separated values parameter can be automatically converted to String array without extra effort:
5309
+
5310
+
[source,java,indent=0]
5311
+
[subs="verbatim,quotes"]
5312
+
----
5313
+
public class MovieRecommender {
5314
+
5315
+
private final String[] catalogs;
5316
+
5317
+
public MovieRecommender(@Value("catalogA,catalogB") String[] catalogs){
5318
+
this.catalogs = catalogs;
5319
+
}
5320
+
}
5321
+
----
5322
+
5323
+
Spring BeanPostProcessor uses ConversionService instance behind the scene to handle the process from converting
5324
+
`@Value` value to the target type.
5325
+
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:
5326
+
5327
+
[source,java,indent=0]
5328
+
[subs="verbatim,quotes"]
5329
+
----
5330
+
@Configuration
5331
+
public class MyConfig {
5332
+
5333
+
@Bean
5334
+
public static ConversionService conversionService() {
5335
+
DefaultFormattingConversionService defaultFormattingConversionService = new DefaultFormattingConversionService();
public PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
5430
+
return new PropertySourcesPlaceholderConfigurer();
5431
+
}
5432
+
}
5433
+
----
5434
+
5435
+
Using the above configuration ensures Spring initialization failure if any of "${}" placeholder could not be resolved.
5436
+
5437
+
NOTE: Spring Boot configures by default a `PropertySourcesPlaceholderConfigurer` bean that will get properties from `application.properties` and `application.yml` files.
0 commit comments