Skip to content

Commit 74fcb0c

Browse files
slem1sdeleuze
authored andcommitted
Add proper @value documentation
Closes gh-23052
1 parent 59d070e commit 74fcb0c

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

src/docs/asciidoc/core/core-beans.adoc

+193
Original file line numberDiff line numberDiff line change
@@ -5243,6 +5243,199 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
52435243
`ApplicationContext`.
52445244

52455245

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();
5336+
defaultFormattingConversionService.addConverter(new MyCustomConverter());
5337+
return defaultFormattingConversionService;
5338+
}
5339+
}
5340+
----
5341+
5342+
SpEL built-in support allows dynamically computed `@Value` at runtime as in the following example shows:
5343+
5344+
[source,java,indent=0]
5345+
[subs="verbatim,quotes"]
5346+
----
5347+
public class MovieRecommender {
5348+
5349+
private final String catalog;
5350+
5351+
public MovieRecommender(@Value("#{systemProperties['user.region'] + 'Catalog' }") String catalog){
5352+
this.catalog = catalog;
5353+
}
5354+
----
5355+
5356+
SpEL also enables to use more complex data structure:
5357+
5358+
[source,java,indent=0]
5359+
[subs="verbatim,quotes"]
5360+
----
5361+
5362+
public class MovieRecommender {
5363+
5364+
private final Map<String, Integer> countOfMoviesPerCatalog;
5365+
5366+
public MovieRecommender(
5367+
@Value("#{{'Thriller': 100, 'Comedy': 300}}") Map<String, Integer> countOfMoviesPerCatalog) {
5368+
this.countOfMoviesPerCatalog = countOfMoviesPerCatalog;
5369+
}
5370+
}
5371+
----
5372+
5373+
A widely spread use of `@Value` is placeholder substitution from `@PropertySource`.
5374+
5375+
A default lenient embedded value resolver is provided by Spring. It will try to resolve the property value and if it
5376+
cannot be resolved, the property name will be injected as the value. For instance, according to the following application.properties content:
5377+
5378+
[source,java,indent=0]
5379+
[subs="verbatim,quotes"]
5380+
----
5381+
catalogName=MovieCatalog
5382+
----
5383+
5384+
The following configuration :
5385+
5386+
[source,java,indent=0]
5387+
[subs="verbatim,quotes"]
5388+
----
5389+
@Configuration
5390+
@PropertySource("classpath:application.properties")
5391+
public class MyConfig {
5392+
}
5393+
----
5394+
5395+
And the class:
5396+
5397+
[source,java,indent=0]
5398+
[subs="verbatim,quotes"]
5399+
----
5400+
public class MovieRecommender {
5401+
5402+
private final String catalog;
5403+
5404+
private final String region;
5405+
5406+
public MovieRecommender(
5407+
@Value("${catalogName}") String catalog, @Value("${region}") String region) {
5408+
this.catalog = catalog;
5409+
this.region = region;
5410+
}
5411+
}
5412+
----
5413+
5414+
The `catalog` attribute will be equal to the `"MovieCatalog"` value.
5415+
The region attribute will be equal the `"${region}"` value.
5416+
5417+
If you want to maintain strict control over non existent values, you should declare a `PropertySourcesPlaceholderConfigurer` bean,
5418+
as the following example shows:
5419+
5420+
5421+
[source,java,indent=0]
5422+
[subs="verbatim,quotes"]
5423+
----
5424+
@Configuration
5425+
@PropertySource("classpath:application.properties")
5426+
public class MyConfig {
5427+
5428+
@Bean
5429+
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.
5438+
52465439

52475440
[[beans-postconstruct-and-predestroy-annotations]]
52485441
=== Using `@PostConstruct` and `@PreDestroy`

0 commit comments

Comments
 (0)