在spring中可以通过下面的方式将配置文件中的项注入到配置中

       <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <!-- standard config -->
                <value>classpath*:application.properties</value>
            </list>
        </property>
    </bean>

    <bean id="cacheManager" class="cn.outofmemory.util.MemCacheManager" init-method="init">
        <property name="nodeList"  value="${memcache.nodelist}"/>
        <property name="initConn" value="${memcache.initConn}"/>
        <property name="minConn" value="${memcache.minConn}"/>
        <property name="maxConn" value="${memcache.maxConn}"/>
        <property name="maxIdle" value="${memcache.maxIdle}"/>
        <property name="maintSleep" value="${memcache.maintSleep}"/>
    </bean>

但是这样的注入没有办法通过程序来访问properties文件中的内容,spring还提供了org.springframework.core.io.support.PropertiesLoaderUtils类可以方便的载入配置文件,如下两行代码:

Resource resource = new ClassPathResource("/application.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);

需要引用下面的类:

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

Spring读取配置文件的更多相关文章

  1. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  2. Spring 读取配置文件(二)

    Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...

  3. Spring 读取配置文件(一)

    注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...

  4. Spring读取配置文件的几种方式

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...

  5. Spring读取配置文件 @Value

    最近在学习Spring如何读取配置文件,记录下方便自己也方便别人: 大致分为两类吧,一种的思路是利用Spring的beanFactoryPostProcessor读取配置文件内容到内存中,也就是应用程 ...

  6. Java中spring读取配置文件的几种方法

    Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...

  7. spring 读取配置文件

    spring读取dubbo xml文件,在本项目内可以调用正常,一旦把改项目打成jar包,供其他项目调用,就会提示找不到配置文件 ClassPathXmlApplicationContext cont ...

  8. Spring读取配置文件,获取bean的几种方式

    BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...

  9. spring读取配置文件PropertyPlaceholderConfigurer类的使用

    这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...

  10. 关于spring读取配置文件的两种方式

    很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...

随机推荐

  1. Unity3D研究院之Android同步方法读取streamingAssets

    版本Unity5.3.3 Android 小米pad1 首先非常感谢 @守着阳光 同学在下面的留言.让我解决了一个大的谜团.. 开始我知道 StreamingAssets 路径是这个 path = & ...

  2. postgresql downgrade issue

    Q: Dear Support Team, If we use ubuntu server to install postgresql9.4, how can we keep original dat ...

  3. SharePoint List 查看器

    using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.ComponentMo ...

  4. Oracle数据库——体系结构

    一.涉及内容 1.了解数据库的物理存储结构和逻辑存储结构 二.具体操作 1.分别使用SQL 命令和OEM 图形化工具查看本地数据库的物理文件,并使用OEM 工具在现有的users 表空间中添加user ...

  5. Oracle数据库概述

    Oracle是一种RDBMS(Relational Database Management System 关系型数据库管理系统),是Oracle公司的核心产品. 2009年4月,Oracle并购了Su ...

  6. maxscript, 数组和字符串下标是从1开始的

    maxscript中数组和字符串下标是从1开始的.

  7. html之label标签

    label标签为input元素定义标注,label标签与相关元素通过id属性绑定在一起. 相关属性: for:规定label绑定到哪个表单元素 form:规定label字段所属的一个或多个表单 示例代 ...

  8. 用Opera Mobile调试手机版网页【转】

    注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...

  9. SVN设置实例

    D:\scmserver\SVNROOT\safeControl,该SVN项目下,有erSystem和hcSystem两个项目.现在人员有两种类型的人,一个内部人员,一个是佰钧成人员. 设置要求: 1 ...

  10. Hadoop学习9--动态增加datanode

    http://www.cnblogs.com/ggjucheng/archive/2012/04/18/2454689.html