很多时候需要将配置信息从程序中剥离粗来,Spring现在提供的方法是通过@Value注解和<context:placeholder>来获取配置文件中的配置信息。这里给出一个简单的例子。

首先在resources文件夹下简历配置文件spring.biz.properties,文件内容为:

dataId=test
versionId=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());
}
}

最后的运行结果如下:

Spring中获取外部配置文件中的属性值的更多相关文章

  1. Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...

  2. 获取UWP配置文件中的版本信息

    原文:获取UWP配置文件中的版本信息 在一般的软件中,我们都会显示当前软件的版本信息.以前作者都是在发版的时候修改一下UWP的配置文件中的版本信息和软件中的版本信息.但是每次这样很麻烦,有时间忘记修改 ...

  3. spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。

    需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@  而不是  ...

  4. Objective-C中变量采用@property的各个属性值的含义

    我们在OC中定义变量,可以自己来定义变量的setter方法来设置变量值,用getter方法来获取变量值.但是当变量数量增多时,还采用手动添加setter/getter方法来操作变量,就会使得程序代码量 ...

  5. 【记录】mybatis中获取常量类中数据

    部分转载,已注明来源: 1.mybatis中获取常量类中数据 <update id="refuseDebt"> UPDATE dt_debt a SET         ...

  6. Web版需求征集系统所得1,servlet中获取checkbox复选框的值

    servlet中获取checkbox复选框的值 </tr> <tr> <td align="right">研究类型</td> < ...

  7. mybatis 中的 xml 配置文件中 ‘<’、 ‘>’ 处理

    mybatis 中的 xml 配置文件中 '<'. '>' 处理 1.使用转义字符将 '<'. '>' 替换掉. 描述 字符 转义字符 小于号 < < 大于号 &g ...

  8. 【Android】12.3 在当前Activity中获取另一个Activity的返回值

    分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 在上一节的示例中,通过StartActivity(Intent)方法启动另一个Activity后,这两个Activ ...

  9. 清除bean中所有非基本数据类型的属性值

    利用beanutils清除javabean中所有非基本数据类型的属性值: import com.google.gson.Gson; import lombok.Data; import org.apa ...

随机推荐

  1. [codevs3044]矩形面积求并

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

  2. Pandas | 09 迭代

    Pandas对象之间的基本迭代的行为取决于类型.当迭代一个系列时,它被视为数组式,基本迭代产生这些值.其他数据结构,如:DataFrame和Panel,遵循类似惯例,迭代对象的键. 简而言之,基本迭代 ...

  3. 踩iviewui中Select 选择器两级联动,重置查询条件时第二级数据无法清除的坑

    小颖公司最近做的项目用的vue+iviewui+axios,在做项目的过程中,遇到一个问题: 二级联动的下拉框,第一个下拉框一直都有值,第二个下拉框是在选择了第一个下拉框之后采取调用ajax获取其值, ...

  4. 洛谷p2827蚯蚓题解

    题目 算法标签里的算法什么的都不会啊 什么二叉堆?? qbxt出去学习的时候讲的,一段时间之前做的,现在才写到博客上的 维护3个队列,队列1表示最开始的蚯蚓,队列2表示每一次被切的蚯蚓被分开的较长的那 ...

  5. TensorFlow多层感知机函数逼近过程详解

    http://c.biancheng.net/view/1924.html Hornik 等人的工作(http://www.cs.cmu.edu/~bhiksha/courses/deeplearni ...

  6. 关于 Keras 模型

    在 Keras 中有两类主要的模型:Sequential 顺序模型 和 使用函数式 API 的 Model 类模型. 1.Sequential 顺序模型 ====>>开始使用 Keras ...

  7. [记录]mscorlib recursive resource lookup bug解决方法

    [Content]Expression: [mscorlib recursive resource lookup bug]Description: Infinite recursion during ...

  8. Oracle GoldenGate 同步数据至Hbase

    该方法没试过,先记下再说 1.软件版本说明: Goldengate 12c 12.2.0.1 for Oracle(源端) Goldengate 12c 12.3.0.1 for Bigdata (目 ...

  9. Linux内核中的IS_ERR()实现

    1.前言 对于任何一个指针来说,必然有三种情况:一种是有效指针,一种是NULL,也就是空指针,一种是错误指针,也就是无效指针,在Linux内核中,所谓的错误指针就是指其已经到达了内核空间的最后一个pa ...

  10. [转帖]k8s 基本使用(下)

    k8s 基本使用(下) https://www.jianshu.com/p/116ce601a60f 如果你没有看过上篇的话,推荐阅读完 k8s 基本使用(上)后再阅读本篇内容. kubectl cr ...