Spring配置文件的使用
2015-11-08 10:13
519 查看
第一步:
在applicationContext.xml配置:
<!-- 同时Enable用${} 获取配置文件的值 && Enable通过@Value注解读取.properties配置 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
<!-- 只是Enable用${} 获取配置文件的值 -->
<!-- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean> -->
第二步:
建立配置文件config.properties,内容如下:
url=www.baidu.com
第三步:
在使用注解获得配置项内容:
第四步:
后面的代码就可以使用userPageSize这个私有变量了,这个字符串的值就是我们配置文件中配置的5.
在applicationContext.xml配置:
<!-- 同时Enable用${} 获取配置文件的值 && Enable通过@Value注解读取.properties配置 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
<!-- 只是Enable用${} 获取配置文件的值 -->
<!-- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean> -->
第二步:
建立配置文件config.properties,内容如下:
url=www.baidu.com
第三步:
在使用注解获得配置项内容:
@Value("#{configProperties['url']}") private String url;
第四步:
后面的代码就可以使用userPageSize这个私有变量了,这个字符串的值就是我们配置文件中配置的5.
相关文章推荐
- Java消息服务(JMS)学习小结
- JDK DualPivotQuicksort 源码解析
- Java解析HTML之HTMLParser使用与详解
- Java理论学时第六节。课后作业。
- 基于Struts2.3.x+Spring3.2.x+Hibernate4.2.x+EasyUI1.3.4+Maven架构的示例程序
- java web开发的标签库
- [CareerCup] 14.2 Try-catch-finally Java中的异常处理
- Java并发编程:Lock
- 简单谈谈java中匿名内部类构造函数
- Java并发编程:synchronized
- Java volatile&synchronized
- Java基础之思维导图2
- Java基础之思维导图1
- Java作业1
- 简单讲解Java的Future编程模式
- maven学习二:maven环境搭建及eclipse安装maven插件
- 用Ant-Ivy来做类似Maven的包管理
- Spring 整合 Quartz 任务调度框架学习笔记
- 深入理解Java中的final关键字
- 35 个 Java 代码性能优化总结