在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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM