一些关键的属性一般都会拿出来作为配置,比如数据库连接等。在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属性的更多相关文章

  1. java中获取系统属性以及环境变量

    java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户 ...

  2. Spring在代码中获取properties文件属性

    这里介绍两种在代码中获取properties文件属性的方法. 使用@Value注解获取properties文件属性: 1.因为在下面要用到Spring的<util />配置,所以,首先要在 ...

  3. js中获取css属性

    直接获取 window.onload = function() { var but = document.getElementById('button'); var div = document.ge ...

  4. JS中获取元素属性的逆天大法

    给大家聊下js中获取元素属性的逆天大法,胆小慎入,切记切记!!! innerHTML.outerHTML.innerText .outerText.value.text().html(),val() ...

  5. springMVC中获取request和response对象的几种方式(RequestContextHolder)

    springMVC中获取request和response对象的几种方式 1.最简单方式:参数 2.加入监听器,然后在代码里面获取 原文链接:https://blog.csdn.net/weixin_4 ...

  6. springMvc中获取通过注解获取properties配置文件(转)

    springMvc的项目中,通过注解@Value获取properties配置文件中的配置,使用该注解必须引入的包: spring-beans-4.1.4.RELEASE.jar 下面是需要在sprin ...

  7. [坑]Spring利用注解@Value获取properties属性为null

    今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...

  8. Spring利用注解@Value获取properties属性为null

    今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到. 如何解决:在使用 ...

  9. SpringBoot利用注解@Value获取properties属性为null

    参考:https://www.cnblogs.com/zacky31/p/8609990.html 今天在项目中想使用@Value来获取Springboot中properties中属性值. 场景:定义 ...

随机推荐

  1. Spring对Quartz的封装实现简单需注意事项

    前段时间在项目中一直使用正常的Quartz突然出现了任务漏跑的情况,由于我以前看过Quartz的内部实现,凭借记忆我觉得是由于Quartz的线程池的使用出现问题导致了故障的发生.为了搞清问题的真相,我 ...

  2. phpmyadmin Wrong permissions on configuration file, should not be world writable!

    巴拉巴拉,实际场景是这样,因为有需要,所以想用django 做个rest服务给其他平台提供服务,发现以前正常的页面都无法运行,奇怪发现有一个页面提示连接不上mysql 难道mysql挂了,打开phpm ...

  3. 用WPF实现查找结果高亮显示

    概述 我们经常会遇到这样的需求:到数据库里查找一些关键字,把带这些关键字的记录返回显示在客户端上.但如果仅仅是单纯地把文本显示出来,那很不直观,用户不能很轻易地看到他们想找的内容,所以通常我们还要做到 ...

  4. 今天心情好,一起探讨下《送给大家的200兆SVN代码服务器》怎么管理我们的VS代码?

    前几天给大家免费送了个200兆SVN代码服务器(今天心情好,给各位免费呈上200兆SVN代码服务器一枚,不谢!),还木有领取的速度戳链接哦! 好几位园友拿到SVN服务器都对其赞不绝口,我也用这个服务器 ...

  5. ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇

    在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP. ...

  6. Git学习笔记(4)——添加远程仓库,克隆远程库,以及库的推送

    本文记录了远程库的连接和库的克隆和推送. 远程仓库简介 Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.有一台机器有一个原始版本库,此后,别的机器可以“克隆”这个原始版本库,而且 ...

  7. IOS Animation-贝塞尔曲线与Layer简单篇(一)

    IOS Animation-贝塞尔曲线与Layer简单篇 swift篇 1.介绍 贝塞尔曲线: 贝塞尔曲线是计算机图形图像造型的基本工具,是图形造型运用得最多的基本线条之一.它通过控制曲线上的四个点( ...

  8. Redis和Memcached的区别详解

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/119.html?1455855360 Redis的作者Salvatore ...

  9. 10 个 Redis 建议/技巧

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/145.html?1455790611 Redis 在当前的技术社区里是非常 ...

  10. Laravel5.0学习--02 实例进阶

    本文以laravel5.0.22为例. 本节以新建一个简单的博客作为实例. 准备工作 数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhos ...