在Springmvc中获取properties属性

时间:2022-05-04
本文章向大家介绍在Springmvc中获取properties属性,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一些关键的属性一般都会拿出来作为配置,比如数据库连接等。在springmvc中也提供了获取property的类,比如@Value来获取。我接触spring很浅,基本上都是百度的问题解决方法,百度到@value的用法,按照说明尝试了两次都失败了。正巧身边又有合适的方法,于是便没有去深入研究为什么失败,这个留在以后研究。下面就是获取代码:

源码来自:https://github.com/thinkgem/jeesite

  1 package com.demo.common.utils;
  2 
  3 import org.apache.commons.io.IOUtils;
  4 import org.slf4j.Logger;
  5 import org.slf4j.LoggerFactory;
  6 import org.springframework.core.io.DefaultResourceLoader;
  7 import org.springframework.core.io.Resource;
  8 import org.springframework.core.io.ResourceLoader;
  9 
 10 import java.io.IOException;
 11 import java.io.InputStream;
 12 import java.util.NoSuchElementException;
 13 import java.util.Properties;
 14 
 15 /**
 16  * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
 17  * Created by Administrator on 2016/2/23.
 18  */
 19 public class PropertiesLoader {
 20     private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
 21     private static ResourceLoader resourceLoader = new DefaultResourceLoader();
 22     private final Properties properties;
 23 
 24     public PropertiesLoader(String... resourcesRaths) {
 25         properties = loadProperties(resourcesRaths);
 26     }
 27 
 28     public Properties getProperties(){
 29         return properties;
 30     }
 31 
 32     /**
 33      * 取出property,但以System的property优先,取不到返回空字符串
 34      */
 35     private String getValue(String key){
 36         String systemProperty = System.getProperty(key);
 37         if (systemProperty!=null){
 38             return systemProperty;
 39         }
 40         if (properties.containsKey(key)){
 41             return properties.getProperty(key);
 42         }
 43         return "";
 44     }
 45 
 46     /**
 47      * 取出String类型的Property,System的优先
 48      * @throws NoSuchElementException
 49      */
 50     public String getProperty(String key){
 51         String value = getValue(key);
 52         if (value==null){
 53             throw new NoSuchElementException();
 54         }
 55         return value;
 56     }
 57 
 58     /**
 59      * 取出String类型的Property,System的优先,null则返回默认值
 60      */
 61     public String getProperty(String key,String defaultValue){
 62         String value = getValue(key);
 63         return value!=null?value:defaultValue;
 64     }
 65 
 66     /**
 67      * 取出Integer类型的Property,System优先
 68      * @throws NoSuchElementException
 69      */
 70     public Integer getInteger(String key){
 71         String value = getValue(key);
 72         if (value==null){
 73             throw new NoSuchElementException();
 74         }
 75         return Integer.valueOf(value);
 76     }
 77         /**
 78      * 取出Integer类型的Property,System优先,null则返回默认值
 79      */
 80     public Integer getInteger(String key,Integer defaultValue){
 81         String value = getValue(key);
 82 
 83         return value!=null?Integer.valueOf(value):defaultValue;
 84     }
 85 
 86     /**
 87      * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
 88      */
 89     public Double getDouble(String key) {
 90         String value = getValue(key);
 91         if (value == null) {
 92             throw new NoSuchElementException();
 93         }
 94         return Double.valueOf(value);
 95     }
 96 
 97     /**
 98      * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
 99      */
100     public Double getDouble(String key, Integer defaultValue) {
101         String value = getValue(key);
102         return value != null ? Double.valueOf(value) : defaultValue;
103     }
104 
105     /**
106      * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
107      */
108     public Boolean getBoolean(String key) {
109         String value = getValue(key);
110         if (value == null) {
111             throw new NoSuchElementException();
112         }
113         return Boolean.valueOf(value);
114     }
115 
116     /**
117      * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
118      */
119     public Boolean getBoolean(String key, boolean defaultValue) {
120         String value = getValue(key);
121         return value != null ? Boolean.valueOf(value) : defaultValue;
122     }
123 
124 
125 
126     /**
127      * 载入多个文件,文件路径使用spring resource格式
128      * @param resourcesRaths
129      * @return
130      */
131     private Properties loadProperties(String[] resourcesRaths) {
132         Properties props = new Properties();
133 
134         for (String location : resourcesRaths) {
135             logger.debug("Loading properties file from:" + location);
136             InputStream is = null;
137             try {
138                 Resource resource = resourceLoader.getResource(location);
139                 is = resource.getInputStream();
140                 props.load(is);
141             } catch (IOException e) {
142                 logger.info("Could not load properties from path:{},{}",location,e.getMessage());
143                 e.printStackTrace();
144             }finally {
145                 IOUtils.closeQuietly(is);
146             }
147         }
148         return props;
149     }
150 }