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. group by实现原理及其作用

    mysql中group by实现方式有三种,松散索引,紧凑索引,临时文件(文件排序). 在网上看了相关的介绍,大部分介绍都比较晦涩难懂,这里说下我的理解. 在学习SQL优化时,我们都知道可以对grou ...

  2. RabbitMQ基础介绍

    非原创,仅作为个人的学习资料,转自 anzhsoft http://blog.csdn.net/anzhsoft/article/details/19563091, 1. 历史 RabbitMQ是一个 ...

  3. C#_事件学习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. typora快捷键之速成笔记

    使用心得:用起来相当的顺手,强烈推荐小伙伴使用该工具,内置快捷功能很贴心 工具下载: https://download.csdn.net/download/lele508994993/10392197 ...

  5. MongoDB 及 Mysql 背后的 B/B+树

    索引是数据库常见的数据结构,每个后台开发人员都应该对索引背后的数据结构有所了解. 本文通过分析B-Tree及B-/+Tree数据结构及索引性能分析及磁盘存取原理尝试着回答一下问题: 为什么B-Tree ...

  6. 手写简化版printf函数

    2019.02.01更新:经同学提醒,myprintf函数应有返回值为输出的字符数. 期末的大作业,手写一个myprintf函数,支持如下一些操作. 也就是  % -(负号控制左右对齐) 数(控制字段 ...

  7. python 操作 Redis

    目录 Redis 模块基本介绍 参考 redis redis-py 的 API 连接 redis 普通连接 连接池 redis 字符串操作 单次设置key-value 批量设置key-value re ...

  8. Linux基础-Shell脚本

    任务一目标:自动部署.初始配置.并启动nginx反向代理服务 把任务拆分来看-自动部署部分,就是先下载安装Nginx 首先建立一个很NB霸气的目录还有一个同样NB霸气的.sh文件 /NBshell/M ...

  9. 【译】第十篇 Integration Services:高级事件行为

    本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...

  10. 某大型网络社区传播性XSS分析

    某大型网络社区传播性XSS分析 这个XSS存在于天涯某个不起眼的子论坛,通过发布新帖子可以触发.         漏洞分析    论坛针对XSS有一定过滤措施,例如转义单双引号,过滤左右尖括号等等.所 ...