Spring中获取外部配置文件中的属性值
来源:互联网 发布:淘宝查权重 编辑:程序博客网 时间:2024/05/21 15:45
很多时候需要将配置信息从程序中剥离粗来,Spring现在提供的方法是通过@Value
注解和<context:placeholder>
来获取配置文件中的配置信息。这里给出一个简单的例子。
首先在resources文件夹下简历配置文件spring.biz.properties,文件内容为:
dataId=testversionId=1.0.1.daily
然后在xml文件中读入该属性值,spring-config.xml文件的内容如下:
<context:property-placeholder location="classpath:spirng.biz.properties"/>
第三步是定义需要这些属性的类,要使用注解必须在xml文件中打开注解驱动,代码为:<context:annotation-config/>
。@Value注解中使用${key}取出key对应的value。TestConfig.java的内容为如下。
package com.javadu.core;import org.springframework.beans.factory.annotation.Value;/** * Created by duqi on 15/9/14. */public class TestConfig { @Value("${dataId}") private String dataId; @Value("${versionId}") private String versionId; private String other; public void setOther(String other){ this.other = other; } public String getDataId(){ return dataId; } public String getVersionId(){ return versionId; }}
在xml文件中定义TestConfig对应的bean,完整的spring-config.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:property-placeholder location="classpath:spirng.biz.properties"/> <bean id="configBean" class="com.javadu.core.TestConfig"> <property name="other" value="otherother"/> </bean></beans>
最后,在App.java类中:启动IoC容器,获取TestBean的实例,调用其开放的接口,代码如下:
package com.javadu.common;import com.javadu.core.TestConfig;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by duqi on 15/9/8. */public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); TestConfig configBean = (TestConfig)context.getBean("configBean"); System.out.println(configBean.getDataId()); System.out.println(configBean.getVersionId()); }}
最后的运行结果如下:
0 0
- Spring中获取外部配置文件中的属性值
- spring配置文件中如何表达属性值中的美元符?
- jsp中获取spring配置文件中的bean
- java客户端程序获取外部spring配置文件
- Spring中外部属性文件
- 2、获取配置文件中的属性
- spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍
- spring配置文件中读取外部参数
- Spring中使用外部的properties配置文件
- spring配置文件中scope属性
- spring配置文件中scope属性
- (五)spring配置文件--使用外部属性文件
- spring中获取配置文件内容
- Java获取配置文件中的属性值通用类
- Spring配置文件中如何使用外部配置文件配置数据库连接
- spring boot 读取配置文件(application.yml)中的属性值
- spring boot 配置文件(application.yml)中的属性值配置
- spring boot 读取配置文件(application.yml)中的属性值
- recv send 阻塞和非阻塞
- 播放器的设计与开发
- CSS框模型中外边距(margin)折叠图文详解
- IOS设置导航栏全局的背景图片、导航栏title文字颜色大小、导航栏左右按钮的颜色。
- 生成DSYM文件
- Spring中获取外部配置文件中的属性值
- CountDownLatch学习笔记
- WinEdt6.0编码设置
- Android显示相关记录
- POJ1681 Painter's Problem【高斯消元法】
- Linux2.6.32驱动笔记(1)驱动开发概述及硬件访问技术
- 怎么在 CSDN 博客中添加QQ 邮箱的邮我功能和网易邮箱的给我写信功能呢?马上给我写信吧2014.09
- 第二周实践项目(2)~程序的多文件组织
- 【matlab】matlab文件操作fopen,fseek,fread,fclose等