zoukankan      html  css  js  c++  java
  • Java中如何获取spring中配置文件.properties中属性值

    通过spring配置properties文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <bean id="propertyConfigurer"
      class="com.hapishop.util.ProjectDBinfoConfigurer">
      <property name="ignoreResourceNotFound" value="true" />
      <property name="locations">
          <list>
              <value>/WEB-INF/config/dbinfo.properties</value>
          </list>
      </property>
    </bean>

    其中class为自己定义的类

     

    自定义类ProjectDBinfoConfigurer

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
     
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
     
    /**
     * 自定义ProjectDBinfoConfigurer返回properties内容
     * @ClassName: ProjectDBinfoConfigurer
     * @Description: TODO (请用一句话描述该类做什么)
     * @author ZhangYQ iitshare@itblood.com
     * @date: 2012-11-20 下午11:48:32
     */
    public class ProjectDBinfoConfigurer extends PropertyPlaceholderConfigurer {
        private static Map ctxPropertiesMap;
     
        @Override
        protected void processProperties(
                ConfigurableListableBeanFactory beanFactoryToProcess,
                Properties props) throws BeansException {
            super.processProperties(beanFactoryToProcess, props);
            ctxPropertiesMap = new HashMap();
            for (Object key : props.keySet()) {
                String keyStr = key.toString();
                String value = props.getProperty(keyStr);
                ctxPropertiesMap.put(keyStr, value);
            }
        }
        public static Object getContextProperty(String name) {
            return ctxPropertiesMap.get(name);
        }
    }

    这样就可以通过ProjectDBinfoConfigurer类来获取properties属性文件中的内容了

    如何获取属性文件的内容

    String url= (String) ProjectDBinfoConfigurer.getContextProperty(“jdbc.url”);

    摘自 : http://blog.itblood.com/java-to-get-the-value-of-the-spring-configuration-file.html

     

     

  • 相关阅读:
    Appium:四:控件
    Appium:三:APP元素定位
    jmeter分布式踩得坑汇总
    Linux环境下进行分布式压测踩过的坑
    记录一次余额迁移的坑(测试角度)
    记录性能测试脚本开发的过程
    jmeter如何设置全局变量
    性能测试,如何得到大量token,并保存在本地文件中
    小程序测试心得
    测试管理三
  • 原文地址:https://www.cnblogs.com/leifei/p/5683517.html
Copyright © 2011-2022 走看看