通过Spring读取properties配置文件的信息 Spring 读取properties
2011-07-26 09:49
731 查看
新建一个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>
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>
相关文章推荐
- 通过Spring读取properties配置文件
- spring上下文读取properties文件配置信息
- 如何在spring中读取properties配置文件里面的信息
- 如何在spring中读取properties配置文件里面的信息
- 在spring中读取properties配置文件里面的信息
- Spring -- 如何在spring中读取properties配置文件里面的信息
- 如何在spring中读取properties配置文件里面的信息
- 通过Spring读取properties配置文件
- spring配置文件读取jdbc.properties的配置信息
- 自定义类在spring中读取properties配置文件信息
- 如何在spring中读取properties配置文件里面的信息
- 如何在spring中读取properties配置文件里面的信息
- 通过spring工厂读取property配置文件
- 【spring配置】——spring读取properties文件
- Java Properties 类读取配置文件信息
- Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中
- Spring 通过配置文件注入 properties文件
- Spring中配置和读取多个Properties文件
- java IO 使用Properties设置键值对配置信息 写入文件和从文件中读取
- Java Properties 类读取配置文件信息