java通过spring获取配置文件,如何通过Spring读取Properties文件

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

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);

}

}

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

public void setApplicationContext(ApplicationContext context) throws BeansException

方法,设置ApplicationContext对象。

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

3 在Spring 的配置文件中配置 ApplicationContextAware  的接口实现类的Bean

4 调用ApplicationContextAware 接口的实现类的getbean 方法, 转换成properties对象, 即可读取

Properties properties = (Properties) SpringContextHolder.getBean("settings");

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

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

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

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

spring读取properties文件

1.方式一

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

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

Spring 如何读取properties文件内容

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

Spring下读取properties文件

由于在spring的xml文件中配置了  /etc/sysconfig/ ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值