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. Java基础-DBCP连接池(BasicDataSource类)详解

    Java基础-DBCP连接池(BasicDataSource类)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程 ...

  2. Jenkins maven 构建乱码,修改file.encoding系统变量编码为UTF-8

    一切都是windows的控制台默认编码GBK问题 情景: 使用jenkins构建,console 输出的中文乱码.代码编码格式是utf-8,因为Jenkins会默认读取当前系统的编码格式,导致构建日志 ...

  3. Windows服务的安装和卸载

    ## install %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ***Cache.WinService.exe # ...

  4. Javascript 常用的工具函数,更新中...

    1.时间戳转为格式化时间 /** * 时间戳转为格式化时间 * @Author chenjun * @DateTime 2017-11-10 * @param {[date]} timestamp [ ...

  5. 多进程+协程 处理IO问题

    from multiprocessing import Pool import gevent,os import time def recursion(n): if n == 1 or n ==2: ...

  6. CSV转excel方法

    步骤一:新建excel文件,数据—>自文本,导入文件 步骤二:选择分隔符,下一步 步骤三:勾选分隔符符合,下一步 步骤四:直接下一步,可在预览里看到格式 步骤五:点击确定,等待数据导入

  7. 【译】第七篇 SQL Server代理作业活动监视器

    本篇文章是SQL Server代理系列的第七篇,详细内容请参考原文 在这一系列的上一篇,你创建并配置SQL Server代理作业.每个作业有一个或多个步骤,可能包含大量的工作流.在这篇文章中,将查看作 ...

  8. python——脚本和print

    脚本和print 1.脚本文件 <Python 基础教程>(第二版)中 P118页,原操作为下: 1 _metaclass_ = type 2 3 class Person: 4 def ...

  9. SolrJ API 官方文档最佳实践

    以下内容译自Solr Wiki官方文档,版权没有,随意转载. Solrj 是一个访问solr的Java客户端.它提供了一个java接口用于添加更新和查询solr索引.本页面介绍SolrJ最新版本1.4 ...

  10. Find Minimum in Rotated Sorted Array I & II

    Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...