知乎专栏 |
默认值 ${变量:默认值}
@Value("${server.name:Windows}") 如果application.properties没有配置server.name那么默认值将是 Windows private String name; @Value("${some.key:my default value}") private String stringWithDefaultValue; @Value("${some.key:true}") private boolean booleanWithDefaultValue; @Value("${some.key:42}") private int intWithDefaultValue; @Value("${some.key:one,two,three}") private String[] stringArrayWithDefaults; @Value("${some.key:1,2,3}") private int[] intArrayWithDefaults; // Using SpEL @Value("#{systemProperties['some.key'] ?: 'my default system property value'}") private String spelWithDefaultValue;
Null 默认值
@Value("${app.name:@null}") // app.name = null private String name;