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
As the annotation java type does not allow non constant value such as null, you can also safely use primitive type parameter:
5294
-
5274
+
And the following `application.properties` file:
5295
5275
[source,java,indent=0]
5296
5276
[subs="verbatim,quotes"]
5297
5277
----
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"]
5278
+
catalog.name=MovieCatalog
5312
5279
----
5313
-
public class MovieRecommender {
5314
5280
5315
-
private final String[] catalogs;
5281
+
In that case, the `catalog` parameter and field will be equal to the `MovieCatalog` value.
5316
5282
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:
5283
+
A default lenient embedded value resolver is provided by Spring. It will try to resolve the
5284
+
property value and if it cannot be resolved, the property name (for example `${catalog.name}`)
5285
+
will be injected as the value. If you want to maintain strict control over non existent
5286
+
values, you should declare a `PropertySourcesPlaceholderConfigurer` bean, as the following
5287
+
example shows:
5326
5288
5327
5289
[source,java,indent=0]
5328
5290
[subs="verbatim,quotes"]
5329
5291
----
5330
-
@Configuration
5331
-
public class MyConfig {
5292
+
@Configuration
5293
+
public class AppConfig {
5332
5294
5333
-
@Bean
5334
-
public static ConversionService conversionService() {
5335
-
DefaultFormattingConversionService defaultFormattingConversionService = new DefaultFormattingConversionService();
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