很多时候需要将配置信息从程序中剥离粗来,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. Active Ball

    Active Ball is a simple game. All you need to do is aim at the food and shoot it, then collect the m ...

  2. Angle Beats Gym - 102361A(计算几何)

    Angle Beats \[ Time Limit: 4000 ms \quad Memory Limit: 1048576 kB \] 题意 给出 \(n\) 个初始点以及 \(q\) 次询问,每次 ...

  3. LeetCode 348. Design Tic-Tac-Toe

    原题链接在这里:https://leetcode.com/problems/design-tic-tac-toe/ 题目: Design a Tic-tac-toe game that is play ...

  4. android开发环境sdk manager无法更新问题

    由于无法fq,也没有vpn,建议各位新手不用sdk manager去安装,直接下载bundle包,就不用去折腾各种开发环境了.推荐bundle下载地址:http://adt.android-studi ...

  5. ESP8266 LUA脚本语言开发: 测试下诱人的程序

    前言 这一节测试一下诱人的程序 实现的功能,APP通过SmartConfig给Wi-Fi模块配网并绑定设备,然后通过MQTT远程控制开发板的继电器, APP显示ESP8266采集的温湿度数据. 简而言 ...

  6. vue 实现模块上移下移 实现排序

    效果图 上移 下移 首先想到的是 数组的相互替换嘛 <template> <div> <div class="box" v-for="(it ...

  7. vue子组件如何向父组件传值

    子组件: <template> <div class="app"> <input @click="sendMsg" type=&q ...

  8. LRU缓存简单实现

    缓存接口定义 /** * 缓存接口 * * @author zhi * */ public interface ICache<K, V> { /** * 添加缓存数据 * * @param ...

  9. linux系统ubuntu中在命令行如何打开图形界面的文件夹

    用linux查看文件列表之类的受到命令行限制,还是不太方便的.在文件夹中打开的话,切换路径又没有linux终端快,于是,需要在命令行窗口中打开文件夹.如何做呢? 来到终端命令行中,cd切换你的路径,使 ...

  10. python 本地时间+8小时

    current_time = (datetime.datetime.now() + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')