|
| 1 | +package com.baeldung.valuewithdefaults; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import javax.annotation.PostConstruct; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.ApplicationContext; |
| 10 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.context.annotation.PropertySource; |
| 13 | +import org.springframework.util.Assert; |
| 14 | + |
| 15 | +import com.google.common.collect.Lists; |
| 16 | +import com.google.common.primitives.Ints; |
| 17 | + |
| 18 | +/** |
| 19 | + * Demonstrates setting defaults for @Value annotation. Note that there are no properties |
| 20 | + * defined in the specified property source. We also assume that the user here |
| 21 | + * does not have a system property named some.key. |
| 22 | + * |
| 23 | + */ |
| 24 | +@Configuration |
| 25 | +@PropertySource(name = "myProperties", value = "valueswithdefaults.properties") |
| 26 | +public class ValuesWithDefaultsApp { |
| 27 | + |
| 28 | + @Value("${some.key:my default value}") |
| 29 | + private String stringWithDefaultValue; |
| 30 | + |
| 31 | + @Value("${some.key:}") |
| 32 | + private String stringWithBlankDefaultValue; |
| 33 | + |
| 34 | + @Value("${some.key:true}") |
| 35 | + private boolean booleanWithDefaultValue; |
| 36 | + |
| 37 | + @Value("${some.key:42}") |
| 38 | + private int intWithDefaultValue; |
| 39 | + |
| 40 | + @Value("${some.key:one,two,three}") |
| 41 | + private String[] stringArrayWithDefaults; |
| 42 | + |
| 43 | + @Value("${some.key:1,2,3}") |
| 44 | + private int[] intArrayWithDefaults; |
| 45 | + |
| 46 | + @Value("#{systemProperties['some.key'] ?: 'my default system property value'}") |
| 47 | + private String spelWithDefaultValue; |
| 48 | + |
| 49 | + |
| 50 | + public static void main(String[] args) { |
| 51 | + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class); |
| 52 | + } |
| 53 | + |
| 54 | + @PostConstruct |
| 55 | + public void afterInitialize() { |
| 56 | + // strings |
| 57 | + Assert.isTrue(stringWithDefaultValue.equals("my default value")); |
| 58 | + Assert.isTrue(stringWithBlankDefaultValue.equals("")); |
| 59 | + |
| 60 | + // other primitives |
| 61 | + Assert.isTrue(booleanWithDefaultValue); |
| 62 | + Assert.isTrue(intWithDefaultValue == 42); |
| 63 | + |
| 64 | + // arrays |
| 65 | + List<String> stringListValues = Lists.newArrayList("one", "two", "three"); |
| 66 | + Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues)); |
| 67 | + |
| 68 | + List<Integer> intListValues = Lists.newArrayList(1, 2, 3); |
| 69 | + Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues)); |
| 70 | + |
| 71 | + // SpEL |
| 72 | + Assert.isTrue(spelWithDefaultValue.equals("my default system property value")); |
| 73 | + |
| 74 | + } |
| 75 | +} |
0 commit comments