spring @value
2015-08-13 20:02
537 查看
转自:http://www.mkyong.com/spring3/spring-value-default-value/
In this tutorial, we will show you how to set a default value for
To set a default value in Spring expression, use
Few examples :
P.S
To set a default value for property placeholder :
Few examples :
config.properties
For “config” bean.
Follow up
Must register a static
XML or annotation, so that Spring
In this tutorial, we will show you how to set a default value for
@Value
1. @Value Examples
To set a default value in Spring expression, use Elvis operator:
#{expression?:default value}
Few examples :
@Value("#{systemProperties['mongodb.port'] ?: 27017}") private String mongodbPort; @Value("#{config['mongodb.url'] ?: '127.0.0.1'}") private String mongodbUrl; @Value("#{aBean.age ?: 21}") private int age;
P.S
@Valuehas been available since Spring 3.0
2. @Value and Property Examples
To set a default value for property placeholder :${property:default value}
Few examples :
//@PropertySource("classpath:/config.properties}") //@Configuration @Value("${mongodb.url:127.0.0.1}") private String mongodbUrl; @Value("#{'${mongodb.url:172.0.0.1}'}") private String mongodbUrl; @Value("#{config['mongodb.url']?:'127.0.0.1'}") private String mongodbUrl;
config.properties
mongodb.url=1.2.3.4 mongodb.db=hello
For “config” bean.
<util:properties id="config" location="classpath:config.properties"/>
Follow up
Must register a static
PropertySourcesPlaceholderConfigurerbean in either
XML or annotation, so that Spring
@Valueknow how to interpret
${}
//@PropertySource("classpath:/config.properties}") //@Configuration @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigIn() { return new PropertySourcesPlaceholderConfigurer(); }
相关文章推荐
- java.lang.ClassNotFoundException: org.springframework.web.content.ContextLoaderListener
- 【Spring笔记】
- Java连接Access 2013
- 字符串全排列算法理解与java实现
- java--关键字和保留字
- eclipse界面以及注释的简单配置
- Java模板方法的设计模式
- Spring的事务管理
- MyEclipse反编译Class文件
- java常见错误及解决方法
- 二叉查找树的java实现
- HBase1.0.1基本操作(java代码)
- 快速排序,归并排序,堆排序的java代码实现
- 使用Spring框架的步骤
- 精通Hibernate——Hibernate中Java对象的状态详解
- 懒得笔记4 spring annotation
- [Java] Majority Element II 找众数2
- eclipse启动报错A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse.
- 深入理解java的finalize
- Eclipse ADT的Custom debug keystore所需证书规格