Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29952544
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-08-19 15:56:28

  1. 通过Spring的配置来读取prperties中的信息
  2. 新建一个properties文件叫test.properties内容如下:
  3. name=studiozero
  4. address=beijing
  5. 首先我们在applicationContext.xml中进行如下配置。需要注意的是,test.properties文件需要和applicationContext.xml放到同一个目录下
  6. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  7.  <property name="location">
  8.   <value>classpath:test.properties</value>
  9.  </property>
  10. </bean>
  11. 做完以上的工作之后,Spring就可读取properties中的信息了。下面我们来看看如果将获取的信息添加的ReadProperties类中
  12. 先将ReadProperties类配置到Spring中去。代码如下
  13. public class ReadProperties{
  14.  private String myName;
  15.  private String myAddress;
  16.  //为name和address提供GETTER和SETTER方法
  17.  
  18.  public static void main(String[] args){
  19.   System.out.println("My name is "+myName);
  20.   System.out.println("My address is "+myAddress);
  21.  }
  22. }
  23. <bean id="readProperties" class="com.sring.test.ReadProperties" scope="request">
  24. </bean>
  25. 然后我们在applicationContext.xml中进行一些配置将test.properties中的myName和myAddress的值赋给ReadProperties中的name和address代码如下
  26. <property name="myName" value="${name}"></property>
  27. <property name="myAddress" value="${address}"></property>
  28. 完整的Spring配置文件如下:
  29. <bean id="readProperties" class="com.sring.test.ReadProperties" scope="request">
  30.  <property name="myName" value="${name}"></property>
  31.  <property name="myAddress" value="${address}"></property>
  32. </bean>
阅读(3460) | 评论(0) | 转发(0) |
0

上一篇:水性漆基础知识

下一篇:雨中登泰山

给主人留下些什么吧!~~