1 在Spring中配置文件中, 配置配置文件的引用

    <util:properties id="settings" location="/WEB-INF/conf/custom.properties"></util:properties>


2 实现一个ApplicationContextAware 的接口实现

public class SpringContextHolder implements ApplicationContextAware {

private static final Logger logger = Logger.getLogger(SpringContextHolder.class);


private static ApplicationContext ctx=null;

public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextHolder.ctx=context;
}

private SpringContextHolder(){
}

public static ApplicationContext getCtx(){
return ctx;
}

public static Object getBean(String name){
return ctx.getBean(name);
}
}

关于 ApplicationContextAware 接口的作用 参见: http://www.blogjava.net/yangjunwei/archive/2013/08/23/403211.html

加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的

public void setApplicationContext(ApplicationContext context) throws BeansException

方法,设置ApplicationContext对象。

前提必须在Spring配置文件中指定该类



3 在Spring 的配置文件中配置 ApplicationContextAware  的接口实现类的Bean
<bean id="springContextHolder" class="com.xxx.common.SpringContextHolder"/> 


4 调用ApplicationContextAware 接口的实现类的getbean 方法, 转换成properties对象, 即可读取
Properties properties = (Properties) SpringContextHolder.getBean("settings");

Ref
其他读取properties文件的方法参考一下链接
java 获取路径的各种方法: http://www.cnblogs.com/guoyuqiangf8/p/3506768.html
java读取properties文件方法和对比: http://shmilyaw-hotmail-com.iteye.com/blog/1899242
Spring自动注入properties文件: http://1358440610-qq-com.iteye.com/blog/2090955

如何通过Spring读取Properties文件的更多相关文章

  1. Java-马士兵设计模式学习笔记-工厂模式-模拟Spring读取Properties文件

    一.目标:读取properties文件,获得类名来生成对象 二.类 1.Movable.java public interface Movable { void run(); } 2.Car.java ...

  2. spring 读取properties文件--通过注解方式

    问题: 需要通过properties读取页面的所需楼盘的名称.为了以后便于修改. 解决: 可以通过spring的 PropertiesFactoryBean 读取properties属性,就不需要自己 ...

  3. spring读取properties文件

    1.方式一 <util:properties id="meta" location="classpath:config/metainfo.properties&qu ...

  4. spring 框架的xml文件如何读取properties文件数据

    spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...

  5. spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

    解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...

  6. Spring 如何读取properties文件内容

    http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置 ...

  7. Spring下读取properties文件

    由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.bea ...

  8. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  9. classpath 及读取 properties 文件

    java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...

随机推荐

  1. Tomcat权威指南-读书摘要系列4

    4. Tomcat的性能调校 测量Web服务器的性能 测试负载的工具 Apache Benchmark Siege JMeter

  2. vs配置SP++3.0

    最近在研究信号处理的C语言算法,突然发现一个西安交大的师兄之前已经写出来了一个完整的库,同样是研究生三年,差别怎么这样大呢. 先从用轮子开始吧. 一.SP++3.0安装及测试 官网下载地址: http ...

  3. Git Cannot rebase: You have unstaged changes.

    Cannot rebase: You have unstaged changes. 那说明你有修改过的文件git stashgit pull --rebase (每次push之前最好这样做一次)git ...

  4. soj1046. Plane Spotting

    1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond o ...

  5. Jquery Ajax自定义无刷新提交表单Form

    Jquery的$.ajax方法可以实现ajax调用,要设置url,post,参数等. 如果要提交现有Form需要写很多代码,何不直接将Form的提交直接转移到ajax中呢. 以前的处理方法 如Form ...

  6. codeforces9D How many trees?

    传送门:http://codeforces.com/problemset/problem/9/D [题解] 树形dp,f(i,j)表示i个节点,高度为j的方案数,枚举左子树大小和哪一个子树高度为j-1 ...

  7. 守卫者的挑战(guard)

    problem Pro 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如果你能通过 ...

  8. js数组排序 reverse()和sort()方法的使用

    WEB前端|js数组排序reverse()和sort()方法的使用,数组中已经存在两个可以直接用来重排序的方法:reverse()和sort(). reverse()方法会对反转数组项的顺序. var ...

  9. nodejs学习:net模块

    官方API文档地址:https://nodejs.org/api/net.html 创建一个server.js var net = require('net'); var PORT = 8099; v ...

  10. sql_injection之基本get注入

    1.代码篇 <?php error_reporting(0); include("../conn.php"); if(isset($_GET['id'])){ $id=$_G ...