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

1 篇文章 0 订阅
1 篇文章 0 订阅

新建一个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>

 



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值