新建一个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>
分享到:
相关推荐
spring读取properties.
spring读取properties配置文件的key值。
Spring 读取properties文件key+value方式.rar
properties_read_demo.zip
主要介绍了Spring Boot的properties配置文件读取,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了spring无法读取properties文件数据问题详解,需要的朋友可以参考下
之前的系统用的是Spring框架,国际化还是比较简单的。但是有一些提示是写在js文件里面的,搜了一些,发现一个js国际化的框架jquery.i18n.properties.js,用了一些还是比较方便的。
主要介绍了Spring用代码来读取properties文件实例解析,具有一定借鉴价值,需要的朋友可以参考下
主要介绍了在SpringBoot下读取自定义properties配置文件的方法,文中涉及到了Spring-boot中读取config配置文件的两种方式,需要的朋友可以参考下
Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
1、在maven工程导入spring坐标 2、dao层的内容与结构 接口 实现类 3、server层的内容与结构 4、创建properties文件 5、配置文件样式 标签头代码:
springboot读取配置文件.rar springboot读取配置文件.rar springboot读取配置文件.rar
osgi多个bundle之间读取同一配置文件,需要单独添加一个用来读取配置文件的bundle
本篇文章主要介绍了Spring中配置和读取多个Properties文件的方式方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
NULL 博文链接:https://nannan408.iteye.com/blog/2256177
今天小编就为大家分享一篇关于Spring加载配置和读取多个Properties文件的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了Spring中属性文件properties的读取与使用详解的相关资料,需要的朋友可以参考下
将“全局变量”和“自定义变量”的配置信息放入spring启动配置和application context。 版本变更说明 1.0.3 完善了说明文档README.md。 将配置文件log4j2.xml和spring-logging.xml从项目目录转移至tool-logging的包...
主要介绍了Spring Boot中配置文件application.properties使用及spring boot读取application.properties文件的方式,需要的朋友参考下吧
在本文中我们给大家整理了关于Spring Boot 的配置文件 application.properties的相关知识点内容,需要的朋友们参考学习下。