很多时候需要将配置信息从程序中剥离粗来,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. 用ant.design的设计注意点---表单

    设计的时候,输入表单项 最好放在一起,不要散落在页面各处,会导致表单项穿插存储数据,穿插验证表单,表单的样式设计要严格按照ant的版来,因为表单是交互最多最容易出错的地方,否则浪费太多工作量 持续集成 ...

  2. chrome-解决该扩展程序未列在 Chrome 网上应用店中

    1.win10添加策略组 复制以下内容到.bat文件中,右键-以管理员身份运行,即可添加策略组 pushd "%~dp0" dir /b C:\Windows\servicing\ ...

  3. 题解 洛谷 P2010 【回文日期】

    By:Soroak 洛谷博客 知识点:模拟+暴力枚举 思路:题目中有提到闰年然后很多人就认为,闰年是需要判断的其实,含有2月29号的回文串,前四位是一个闰年那么我们就可以直接进行暴力枚举 一些小细节: ...

  4. Linux下rpm及yum安装jdk

    笔者使用的Linux操作系统为CentOS7,Linux中有三种安装jdk的方式,在这里笔者主要介绍两种方式,即rpm和yum安装及卸载jdk,还有一种是直接从官网下载 tar.gz 压缩文件上传到L ...

  5. CentOS设置时区

    1.使用date命令查看当前时间 2.已软连接的方式设置时区 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  6. sonarqube使用maven进行代码分析

    修改setting.xml文件,增加并激活profile <profile> <id>sonar</id> <properties> <sonar ...

  7. CentOS环境设置Hbase自启动

    1.在/etc/init.d/目录创建hbase文件 #!/bin/bash # chkconfig: #export JAVA_HOME=/usr/local/jdk1.8.0_191 HBASE_ ...

  8. Sourcetree 集成 Azure DevOps Server(Git)

    概述- SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端管理工具,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交 ...

  9. 利用docker实现私有镜像仓库

    利用docker实现私有镜像仓库 在linux服务器上安装了docker过后,可以拉取docker镜像仓库: docker pull registry 再执行命令让镜像run起来: docker ru ...

  10. CentOS忘记密码修改方案以及centos卡在开机登录界面,命令失效的解决方法

    CentOS忘记密码修改方案 应用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于CentOS7.2环境进行操作的,由于CentOS的版本之间是有差异的,继续之前请先确定 ...