获取Spring项目配置文件元素
在开发中有时候要获取配置文件里的值,通常可以利用如下方式来读取:
public class PropertyUtil {
private static Properties p = new Properties();
static {
init("config.properties");
}
/**
* read config file and set vlaue to Properties p
* @param propertyFileName 文件名
*/
protected static void init(String propertyFileName)
{
InputStream in = null;
try {
in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
if (in != null){
p.load(in);
}
} catch (IOException e){
System.err.println("load ["+propertyFileName+"] error!"+e.getMessage());
} finally {
IOUtils.closeQuietly(in);
}
}
public static String getValue(String key)
{
String value = null;
try {
value = p.getProperty(key);
} catch (Exception e) {
//
}
return value;
}
}
下面推荐一个利用PropertyPlaceholderConfigurer来获取配置文件元素的方法:
package com.spring.common.util; import java.util.HashMap;
import java.util.Map;import java.util.Properties; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public final class PropertiesUtil extends PropertyPlaceholderConfigurer { private static Map<String, String> PropertiesMap;
private static Properties properties = new Properties(); @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
properties.putAll(props);
PropertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = (String) key;
String value = props.getProperty(keyStr);
PropertiesMap.put(keyStr, value);
}
} /**
* Get a value based on key , if key does not exist , null is returned
* @param key
* @return
*/
public static String getString(String key) {
try {
return PropertiesMap.get(key);
} catch (Exception e) {
return null;
}
} /**
* 根据key获取值
*
* @param key
* @return
*/
public static int getInt(String key) {
return Integer.parseInt(PropertiesMap.get(key));
} /**
* 根据key获取值
*
* @param key
* @param defaultValue
* @return
*/
public static int getInt(String key, int defaultValue) {
String value = PropertiesMap.get(key);
if (StringUtils.isBlank(value)) {
return defaultValue;
}
return Integer.parseInt(value);
} public static boolean getBoolean(String key) {
return getBoolean(key, false);
} /**
* 根据key获取值
* @param key
* @param defaultValue
* @return
*/
public static boolean getBoolean(String key, boolean defaultValue) {
String value = PropertiesMap.get(key);
if (StringUtils.isBlank(value)) {
return defaultValue;
}
return Boolean.parseBoolean(value);
} }
需要在spring.xml文件里引入一下配置文件,注意类名全路径不要写错:
<!-- 引入属性配置文件 -->
<bean class="com.spring.common.util.PropertiesUtil">
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
在需要的地方调用下getString方法:
String password = PropertiesUtil.getString("password");
获取Spring项目配置文件元素的更多相关文章
- Java中如何获取spring中配置文件.properties中属性值
通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer" class="co ...
- 监听器如何获取Spring配置文件(加载生成Spring容器)
Spring容器是生成Bean的工厂,我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个 ...
- 监听器如何获取Spring配置文件
我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...
- Spring项目的配置文件们(web.xml context servlet springmvc)
我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...
- maven Spring获取不到配置文件
如题: 如果在maven项目中,Spring获取不到配置文件, 把配置文件放到.src/main/resource文件夹下即可 import org.springframework.context.s ...
- spring项目读取配置文件
Spring项目在运用中读取配置文件有两种方式: 通过项目的配置文件读取 在spring-context.xml里面加入以下代码 在运用到的类里面加入 @Value("#{configPro ...
- Spring读取配置文件,获取bean的几种方式
BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- Spring中获取web项目的根目录
spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能; WebAppRootListe ...
随机推荐
- struts2 OGNL(Object-Graph Navigation Language) 井号,星号,百分号
1.“#”主要有三种用途: 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():可以访问这几个ActionContext中的属性. parameter ...
- 【其他】csv文件打开是乱码,怎么办?
csv文件打开是乱码,怎么办?管用的方法,一个就够 工作中,将python生成的中间结果文件写入CSV,经常这么干是不是?文件保存下来后用excel打开,出现了乱码情况,真心烦.为什么? CSV是用U ...
- 【深入理解javascript】原型
1.一切都是对象 一切(引用类型)都是对象,对象是属性的集合 typeof函数输出的一共有几种类型,在此列出: function show(x) { console.log(typeof(x)); / ...
- C#基础笔记(第十一天)
1.复习字符串(1)字符串的不可变性(2)字符串的方法:1)Split() 分割 把字符串中不想要的内容分割掉 返回一个字符串类型的数组 可以添加StringSplitOption.RemoveEmp ...
- python类内部调用自己的成员函数必须加self
class A: def a(self): print("hello world") def b(self): return self.a() 上面的self.a()中self是不 ...
- sap gui 使用方法, sap logon
1:打断点:在程序保存并激活之后,可以打内部或外部断点. 如图示. 2: display 展示程序,不能修改. 3: 查看创建的类的结构,使用more>display object list ...
- [py]python自省工具
参考 在日常生活中,自省(introspection)是一种自我检查行为.自省是指对某人自身思想.情绪.动机和行为的检查.伟大的哲学家苏格拉底将生命中的大部分时间用于自我检查,并鼓励他的雅典朋友们也这 ...
- iOS开发--UILabel根据内容自动调整高度
写法一:对象方法,传入:字体/最大尺寸. 即可得到宽高, 最大尺寸主要限制宽度,如果是一行就给个{MAXFLOAT,MAXFLOAT};如果是多行就限制X值,Y值随便给 - (CGSize)sizeW ...
- Verilog篇(四)时序模型
时序模型:仿真器的时间推进模型,它反映了推进仿真时间和调度事件的方式. 1)门级时序模型:适用于分析所有的连续赋值语句,过程连续赋值语句,门级原语,用户自定义原语. 特点:任意时刻,任意输入变化都将重 ...
- EPD的驱动
整个e-ink技术的生产流程:1997年从MIT媒体实验室走出来的E-INK公司成立并专注于研发具有良好阅读体验的电子纸.其中最著名的产品就是Vizplex 电子墨水.E-INK提供电子墨水给Pane ...