在Springmvc中获取properties属性
一些关键的属性一般都会拿出来作为配置,比如数据库连接等。在springmvc中也提供了获取property的类,比如@Value来获取。我接触spring很浅,基本上都是百度的问题解决方法,百度到@value的用法,按照说明尝试了两次都失败了。正巧身边又有合适的方法,于是便没有去深入研究为什么失败,这个留在以后研究。下面就是获取代码:
源码来自:https://github.com/thinkgem/jeesite
package com.demo.common.utils; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties; /**
* Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
* Created by Administrator on 2016/2/23.
*/
public class PropertiesLoader {
private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private final Properties properties; public PropertiesLoader(String... resourcesRaths) {
properties = loadProperties(resourcesRaths);
} public Properties getProperties(){
return properties;
} /**
* 取出property,但以System的property优先,取不到返回空字符串
*/
private String getValue(String key){
String systemProperty = System.getProperty(key);
if (systemProperty!=null){
return systemProperty;
}
if (properties.containsKey(key)){
return properties.getProperty(key);
}
return "";
} /**
* 取出String类型的Property,System的优先
* @throws NoSuchElementException
*/
public String getProperty(String key){
String value = getValue(key);
if (value==null){
throw new NoSuchElementException();
}
return value;
} /**
* 取出String类型的Property,System的优先,null则返回默认值
*/
public String getProperty(String key,String defaultValue){
String value = getValue(key);
return value!=null?value:defaultValue;
} /**
* 取出Integer类型的Property,System优先
* @throws NoSuchElementException
*/
public Integer getInteger(String key){
String value = getValue(key);
if (value==null){
throw new NoSuchElementException();
}
return Integer.valueOf(value);
}
/**
* 取出Integer类型的Property,System优先,null则返回默认值
*/
public Integer getInteger(String key,Integer defaultValue){
String value = getValue(key); return value!=null?Integer.valueOf(value):defaultValue;
} /**
* 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
} /**
* 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
*/
public Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
} /**
* 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
} /**
* 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
} /**
* 载入多个文件,文件路径使用spring resource格式
* @param resourcesRaths
* @return
*/
private Properties loadProperties(String[] resourcesRaths) {
Properties props = new Properties(); for (String location : resourcesRaths) {
logger.debug("Loading properties file from:" + location);
InputStream is = null;
try {
Resource resource = resourceLoader.getResource(location);
is = resource.getInputStream();
props.load(is);
} catch (IOException e) {
logger.info("Could not load properties from path:{},{}",location,e.getMessage());
e.printStackTrace();
}finally {
IOUtils.closeQuietly(is);
}
}
return props;
}
}
在Springmvc中获取properties属性的更多相关文章
- java中获取系统属性以及环境变量
java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户 ...
- Spring在代码中获取properties文件属性
这里介绍两种在代码中获取properties文件属性的方法. 使用@Value注解获取properties文件属性: 1.因为在下面要用到Spring的<util />配置,所以,首先要在 ...
- js中获取css属性
直接获取 window.onload = function() { var but = document.getElementById('button'); var div = document.ge ...
- JS中获取元素属性的逆天大法
给大家聊下js中获取元素属性的逆天大法,胆小慎入,切记切记!!! innerHTML.outerHTML.innerText .outerText.value.text().html(),val() ...
- springMVC中获取request和response对象的几种方式(RequestContextHolder)
springMVC中获取request和response对象的几种方式 1.最简单方式:参数 2.加入监听器,然后在代码里面获取 原文链接:https://blog.csdn.net/weixin_4 ...
- springMvc中获取通过注解获取properties配置文件(转)
springMvc的项目中,通过注解@Value获取properties配置文件中的配置,使用该注解必须引入的包: spring-beans-4.1.4.RELEASE.jar 下面是需要在sprin ...
- [坑]Spring利用注解@Value获取properties属性为null
今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...
- Spring利用注解@Value获取properties属性为null
今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...
- SpringBoot利用注解@Value获取properties属性为null
参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...
随机推荐
- maven配置发布仓库
首先,在工程的pom.xml中添加仓库信息 <distributionManagement> <repository> <id>releases</id> ...
- SQL入门经典(九) 之自定义函数
UDF和存储过程很类似,用户自定义函数是一组有序的T-SQL语句,这些语句被预先优化和编译,并且可以作为一个单元来测试调用.UDF和存储过程的主要区别在于结果返回方式,为了能支持更多返回值,UDF比存 ...
- centos7 memcached+memagent 集群
1. 安装libevent wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libe ...
- ASP.NET 5系列教程 (五):在Visual Studio 2015中使用Grunt、Bower开发Web程序
基于Visual Studio 2015,你可以: 方便的管理前端包,如jQuery, Bootstrap, 或Angular. 自动运行任务,如LESS.JavaScript压缩.JSLint.Ja ...
- SQL SERVER--DBA 常用到的一些脚本
自己整理了一些常用到的脚本,希望对各位有用 下载地址 --================================== 妹子不能少,是吧 BTW, 妹子是我辛苦百度来的,请不要求种求介绍各种求 ...
- [51单片机] 以PWM控制直流电机为例建一个简单的51工程框架
目录 1)功能概述 2)引脚连接 3)框架介绍 4)模块说明 5)复用规则 6)工程链接 1)功能概述 名称:独立按键控制直流电机调速 内容:对应的电机接口需用杜邦线连接到uln2003电机控制端; ...
- Propagation of Visual Entity Properties Under Bandwidth Constraints
1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...
- MYSQL 大文件无法导入的问题。
1. 设置maxpacket. 要在[mysqld]标签下.这个疏忽了,就会发现没效果. 基本网上的都没说清,要看stackoverflow. Change in the my.ini file. I ...
- import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包(转载)
http://www.xuebuyuan.com/2008608.html 在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCode ...
- servlet到底是什么?
Servlet(Server Applet),全称Java Servlet,是用Java编写的服务器端程序.指任何实现了这个Servlet接口的类或者实现Servlet接口的类,当然或者实现接口继承方 ...