Spring读取properties资源文件
我们知道可以通过读取资源文件流后加载到Properties对象,再使用该对象方法来获取资源文件。现在介绍下利用Spring内置对象来读取资源文件。
系统启动时加载资源文件链路:web.xml --> spring-core.xml --> sysconfig.properties
接下来直接看代码吧
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-core.xml</param-value>
</context-param>
spring-core.xml
<!-- 加载properties里的内容 -->
<bean id="PropertyConfig" class="com.wulinfeng.PropertiesConfig">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:sysconfig.properties</value>
</list>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>
PropertiesConfig.java
public class PropertiesConfig extends PropertyPlaceholderConfigurer
{
private static Map<String, String> propertyMap; @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException
{
super.processProperties(beanFactoryToProcess, props);
if (propertyMap == null || propertyMap.size() == 0)
{
propertyMap = new HashMap<String, String>(); for (Object key : props.keySet())
{
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}
}
} public static String getProperty(String name,String def)
{
if (propertyMap == null || propertyMap.isEmpty() || null == propertyMap.get(name))
{
return def;
}
return propertyMap.get(name);
} public static String getProperty(String name)
{
if (propertyMap == null || propertyMap.isEmpty())
{
return null;
}
return propertyMap.get(name);
} }
注意这里需要继承Spring的PropertyPlaceholderConfigurer类。
Spring读取properties资源文件的更多相关文章
- 在服务端中,读取properties资源文件中的数据
1.获取到资源的路径 2.读取数据 //properties文件对象 Properties properties = new Properties(); //通过HttpServletRequest ...
- Java中读取properties资源文件
一.通过ResourceBundle来读取.properties文件 /** * 通过java.util.resourceBundle来解析properties文件. * @param String ...
- spring mvc读取properties资源文件夹中文乱码问题
通过在applicationContext.xml和springmvc.xml中配置 <bean class="org.springframework.beans.fac ...
- 六种方式读取properties资源文件
conf.properties文件内容: reportStationName=xx供电局 JBM=0318 文件路径: 其中xxx为项目名 import java.io.BufferedInputSt ...
- 读取web工程中.properties资源文件的模板代码
读取web工程中.properties资源文件的模板代码 // 读取web工程中.properties资源文件的模板代码 private void test2() throws IOException ...
- Spring3.x 获取properties资源文件的值
Spring3.x 获取properties资源文件的值有两种方式: 第一种:使用<context:property-placeholder />标签 <context:prop ...
- 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】
首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...
- JNI读取assets资源文件
源自:http://www.rosoo.net/a/201112/15459.html assets目录底下的文件会被打包到一个apk文件里,这些资源在安装时他们并没被解压,使用时是直接从apk中读取 ...
- Java学习笔记——JDBC读取properties属性文件
Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...
随机推荐
- Zookeeper初步了解
Zookeeper初步了解: Zookeeper实现了许多复杂的事情,例如实现了Zookeeper Atomic Broadcasting Protocal来广播状态信息的变化,Fast Paxas ...
- NumPy矩阵库
NumPy - 矩阵库 NumPy 包包含一个 Matrix库numpy.matlib.此模块的函数返回矩阵而不是返回ndarray对象. matlib.empty() matlib.empty()函 ...
- angular components
最近写了一套angular 组件,希望大家支持: Github: https://github.com/zhantewei2/ng-ztw webSite:http://39.108.193.57:3 ...
- mysql数据库(二):查询(SELECT)
一. 数据库查询—查询(SELECT) 单表查询 多表联合查询 二. 查询—单表查询 查询特定字段: select <字段1,字段2,...> from <表名>; 示例:查询 ...
- java开发工具idea,在install时候报错The packaging for this project did not assign a file to the build artifact
intellij中install报错:The packaging for this project did not assign a file to the build artifact 原因是run ...
- RedHat Linux服务器安全配置细节
1.概述 Linux服务器版本:RedHat Linux AS 2.1 对于开放式的操作系统---Linux,系统的安全设定包括系统服务最小化.限制远程存取.隐藏重要资料.修补安全漏洞.采用安全工具以 ...
- [转载]SVN trunk、branch、tag的用法
Subversion有一个很标准的目录结构,是这样的. 比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是 svn://proj/|+-trunk+-branches+-t ...
- C# 中字段和属性的使用时机
在C#中,我们可以非常自由的.毫无限制的访问公有字段,但在一些场合中,我们可能希望限制只能给字段赋于某个范围的值.或是要求字段只能读或只能写,或是在改变字段时能改变对象的其他一些状态,这些单靠字段是无 ...
- HAWQ取代传统数仓实践(九)——维度表技术之退化维度
退化维度技术减少维度的数量,简化维度数据仓库模式.简单的模式比复杂的更容易理解,也有更好的查询性能. 有时,维度表中除了业务主键外没有其它内容.例如,在本销售订单示例中,订单维度表除了订 ...
- Compiling OpenGL games with the Flash C Compiler (FlasCC)
Compiling OpenGL games with the Flash C Compiler (FlasCC) In this article I show how to use the Flas ...