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 ...
随机推荐
- centos7 -lvm卷组
老忘,记一下 基本的逻辑卷管理概念: PV(Physical Volume)- 物理卷 物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备. ...
- HIVE 编写自定义函数UDF
一 新建JAVA项目 并添加 hive-exec-2.1.0.jar 和hadoop-common-2.7.3.jar hive-exec-2.1.0.jar 在HIVE安装目录的lib目录下 had ...
- js 小复习2
1.数组 findIndex() indexOf() // findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. function isBigEnough(ele ...
- shell read
#!/bin/bash read -p "Enter your account:" acct #提示用户输入用户名read -s -p "Enter your passw ...
- Autolayout .Compact or .Regular [iPhone/iPad]
- Firefox 下载、附加组件、Flash插件、缓存位置(附加Chrome下载和Opera下载)
Firefox 下载的FTP页面: http://ftp.mozilla.org/pub/firefox/releases/ Firefox下载官方页面: https://www.mozilla.or ...
- 使用命令行启动spring boot项目
1.找到项目路径 cd target 2.敲命令--------java -jar xxxx.jar ------------------------------------------------- ...
- 【IDEA】笔记
引言 IDEA是JAVA开发的一个神器,熟悉它能极大提高我们的开发效率.正所谓工欲善其事,必先利其器. 快捷键 快捷键 介绍 Ctrl + F 在当前文件进行文本查找 (必备) Ctrl + R 在当 ...
- Tinymce group plugin
本文介绍一个tinymce插件,用来组合显示下拉的按钮.基于4.x,不兼容3.x. 以前 配置toolbar功能按钮 需要 toolbar1: "code undo red ...
- vsftp中的local_umask和anon_umask
umask是unix操作系统的概念,umask决定目录和文件被创建时得到的初始权限umask = 022 时,新建的目录 权限是755,文件的权限是 644umask = 077 时,新建的目录 权限 ...