`
dyllove98
  • 浏览: 1383988 次
  • 性别: Icon_minigender_1
  • 来自: 济南
博客专栏
73a48ce3-d397-3b94-9f5d-49eb2ab017ab
Eclipse Rcp/R...
浏览量:38377
4322ac12-0ba9-3ac3-a3cf-b2f587fdfd3f
项目管理checkList...
浏览量:78736
4fb6ad91-52a6-307a-9e4f-816b4a7ce416
哲理故事与管理之道
浏览量:131906
社区版块
存档分类
最新评论

通过Spring读取properties配置文件的信息 Spring 读取properties

阅读更多

新建一个properties文件叫test.properties内容如下:
name=studiozero
address=beijing
首先我们在applicationContext.xml中进行如下配置。需要注意的是,test.properties文件需要和applicationContext.xml放到同一个目录下
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location">
  <value>classpath:test.properties</value>
 </property>
</bean>
做完以上的工作之后,Spring就可读取properties中的信息了。下面我们来看看如果将获取的信息添加的ReadProperties类中
先将ReadProperties类配置到Spring中去。代码如下
public class ReadProperties{
 private String myName;
 private String myAddress;
 //为name和address提供GETTER和SETTER方法
 
 public static void main(String[] args){
  System.out.println("My name is "+myName);
  System.out.println("My address is "+myAddress);
 }
}
<bean id="readProperties" class="com.sring.test.ReadProperties" scope="request"> 
</bean>
然后我们在applicationContext.xml中进行一些配置将test.properties中的myName和myAddress的值赋给ReadProperties中的name和address代码如下
<property name="myName" value="${name}"></property>
<property name="myAddress" value="${address}"></property>
完整的Spring配置文件如下:
<bean id="readProperties" class="com.sring.test.ReadProperties" scope="request">
 <property name="myName" value="${name}"></property>
 <property name="myAddress" value="${address}"></property>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics