`
hepeng_8
  • 浏览: 80590 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

通过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