Spring-@value用法详解
来源:互联网 发布:淘宝白底图片制作 编辑:程序博客网 时间:2024/06/01 19:12
为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性。
一、两种使用方法
1、@Value("#{configProperties['key']}")
2、@Value("${key}")
二、配置
2.1 @Value("#{configProperties['key']}")使用
2.1.1配置文件:
配置方法1:<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:value.properties</value> </list> </property></bean>
配置方法2:<util:properties id="configProperties" location="classpath:value.properties"></util:properties>注:配置1和配置2等价,这种方法需要util标签,要引入util的xsd:
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
value.properties
key=1
ValueDemo.java
@Componentpublic class ValueDemo { @Value("#{configProperties['key']}") private String value; public String getValue() { return value; }}
2.2 @Value("${key}")使用
2.2.1 配置文件
1、在2.1.1的配置文件基础上增加:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/></bean>
2、直接指定配置文件,完整的配置:
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:value.properties</value> </array> </property></bean>
ValueDemo.java
@Componentpublic class ValueDemo { @Value("${key}") private String value; public String getValue() { return value; }}
阅读全文
0 0
- Spring-@value用法详解
- Spring @Value用法
- Spring @Value用法
- Spring @Value用法
- Spring 注解@Value详解
- Spring中@Value的用法
- Spring @Value注解的用法
- Spring @value注解的错误用法
- Spring @Value 用法小结,#与$的区别
- Spring @Value 用法小结,#与$的区别
- spring ref和value的用法
- spring mvc中的@RequestMapping value用法小结
- spring(基础10) 注解@Value详解
- spring(基础10) 注解@Value详解
- spring RestTemplate用法详解
- spring RestTemplate用法详解
- spring RestTemplate用法详解
- spring RestTemplate用法详解
- RTP 协议——001
- Hadoop NameNode元数据相关文件目录解析
- Android.mk引用aar
- 从4.4到7.0三个大版本的变化
- HttpWebRequest中的ContentType详解
- Spring-@value用法详解
- LinkedList源码分析
- ZooKeeper管理分布式环境中的数据
- ImportError: No module named _jythonlib
- [7] Linux软件包安装和卸载
- android与java浅谈this与activity.this
- 简单的jQuery无缝向上滚动效果
- Python开发资源速查表
- Python