通过.properties集中管理spring框架中的路径

  •  

    最近最项目:由于用到了太多的路径,动态写也不大方便(由于后期肯定要改)

    所以刚开始为了测试直接写死了:由于越来越多,将来修改写来也不太好找,就专门做了一个.properties文件如下图:

    一:新建一个.properties文件

    二:然后就是要在applicationContext.xml中配置.properties的扫描

     

    
    

     

    三:封装javaBean

     

    四:用Junit测试:

     


     

    五:测试,控制台输出如下:

     

    说明配置文件已经正确加载,可以使用了…….

     

    在网上看到的其他的一些方式:

    方法一:

    
    

    1. public static String getValue(String fileNamePath, String key)throws IOException {    
    1.        Properties props = new Properties();    
    2.        InputStream in = null;    
    3.        try {    
    4.            in = new FileInputStream(fileNamePath);    
    5.            // 如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中    
    6.            //in = propertiesTools.class.getResourceAsStream(fileNamePath);    
    7.            props.load(in);    
    8.            String value = props.getProperty(key);    
    9.            // 有乱码时要进行重新编码    
    10.            // new String(props.getProperty("name").getBytes("ISO-8859-1"), "GBK");    
    11.            return value;    
    12.    
    13.        } catch (IOException e) {    
    14.            e.printStackTrace();    
    15.            return null;    
    16.    
    17.        } finally {    
    18.            if (null != in)    
    19.                in.close();    
    20.        }    
    21.    }  

    方法二:

    通过spring配置properties文件

    1. <bean id="propertyConfigurer"  
    1.   class="com.hapishop.util.ProjectDBinfoConfigurer">  
    2.   <property name="ignoreResourceNotFound" value="true" />  
    3.   <property name="locations">  
    4.       <list>  
    5.           <value>app.properties</value>  
    6.       </list>  
    7.   </property>  
    1. </bean>  

     

    自定义PropertiesConfigurer

    1. import java.util.HashMap;  
    2. import java.util.Map;  
    3. import java.util.Properties;  
    1.    
    1. import org.springframework.beans.BeansException;  
    2. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
    3. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;  
    1.    
    2.   
    1. public class PorpertiesConfigurer extends PropertyPlaceholderConfigurer {  
    1.     private static Map ctxPropertiesMap;  
    2.    
    3.     @Override  
    4.     protected void processProperties(  
    5.             ConfigurableListableBeanFactory beanFactoryToProcess,  
    6.             Properties props) throws BeansException {  
    7.         super.processProperties(beanFactoryToProcess, props);  
    8.         ctxPropertiesMap = new HashMap();  
    9.         for (Object key : props.keySet()) {  
    10.             String keyStr = key.toString();  
    11.             String value = props.getProperty(keyStr);  
    12.             ctxPropertiesMap.put(keyStr, value);  
    13.         }  
    14.     }  
    15.     public static Object getContextProperty(String name) {  
    16.         return ctxPropertiesMap.get(name);  
    17.     }  
    18. }  

     

     

     

     

     

     

     

     

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值