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

     

     

  • 相关阅读:
    他人监控相关博客
    cassandra高级操作之JMX操作
    【原创】官方文档-hive 启动命令
    oracle必须启动的服务
    【官方文档】elasticsearch中的API
    Oracle 11g即时客户端在windows下的配置
    Oracle 使用SQL*Plus连接数据库
    Oracle 关闭数据库(未使用Oracle Restart)
    Oracle 启动实例(instance)、打开数据库
    Oracle win32_11gR2_database在Win7下的安装与卸载
  • 原文地址:https://www.cnblogs.com/leifei/p/5683517.html
Copyright © 2011-2022 走看看