properties配置载入
来源:互联网 发布:java中单选按钮 编辑:程序博客网 时间:2024/06/08 07:56
.properties文件是一种常用的KEY-VALUE键值对形式的配置文件。
spring通过加载配置文件获取键值对,然后value值赋予给和key对应的属性上,从而使得程序能运用到这个配置属性。
下面看一下例子:
user.properties配置文件
user.username=yangjiachanguser.password=yjcyjcyjc
spring配置
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations" > <list> <value>classpath*:props/user.properties</value> </list> </property></bean>
测试类:
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Value;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Created by yangjiachang on 2016/7/22. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:spring-context.xml")public class PropertiesDemoTest extends AbstractJUnit4SpringContextTests { @Value("${user.username}") private String username; @Value("${user.password}") private String password; @Test public void test(){ System.out.println(username); System.out.println(password); }}
运行结果:
yangjiachang
yjcyjcyjc
以上说明已经通过注解的方式成功的从user.properties配置文件中取得了对应的值。
实际上,原理也是通过spring注入的方式实现的,默认情况下,spring注入的为单例,因此只有该实例会根据配置赋值,new出来的实例是没有的。
0 0
- properties配置载入
- 载入Properties的方法
- 载入和保存properties文件
- 工具类: Properties文件载入
- Properties文件载入工具类
- Properties文件载入工具类
- Properties属性文件载入、读取工具类
- Java 无法载入配置
- properties配置
- 重启nginx载入配置
- struts2 struts.properties配置
- struts.properties配置
- log4j.properties配置
- struts2 properties配置详解
- jndi.properties文件配置
- log4j.properties配置
- log4j.properties文件配置
- oscache.properties 配置说明
- 堆和栈(1)
- VoLTE技术中的会话持续性-SRVCC
- 一张图将Activity与Fragment生命周期展现的淋漓尽致
- 网络编程里几个结构
- 20160722关于保存应用图片到本地的相册
- properties配置载入
- Android Studio给项目添加第三方库的方法
- 堆和栈(2)
- iOS-自定义标签及其控件(一)
- mongodb 监听远程连接
- java的深拷贝与浅拷贝
- Spark Transformation —— distinct 算子
- 文件排序Java工具类(未测试)
- 微信开发环境,高级接口可用