zoukankan      html  css  js  c++  java
  • spring properties的读取方式

    第一种:context:property-placeholder

    <context:property-placeholder location="classpath:jdbc.properties"/>

    第二种:PropertyPlaceholderConfigurer, 与第一种等价

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>

    第三种: PropertiesFactoryBean

    <bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <array>
                    <value>classpath:jdbc.properties</value>
                </array>
            </property>
        </bean>

    第四种: util:properties

    <util:properties id="propertiesReader" location="classpath:jdbc.properties"/>

    第五种: 实现PropertyPlaceholderConfigurer在加载上下文的时候暴露properties到自定义子类的属性中以供程序中使用

    <bean id="propertyConfigurer" class="com.*.util.PropertyConfigurer">
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
            <property name="ignoreResourceNotFound" value="true"/>
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
    package com.*.util;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    import java.util.Properties;
    
    /**
     * @author dhm
     * @desc
     * @date 2017/11/21
     */
    
    public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
        private Properties props;
    
        @Override
        protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
            super.processProperties(beanFactoryToProcess, props);
            this.props = props;
        }
    
        public String getProperty(String key) {
            return props.getProperty(key);
        }
    }

    第六种: 原始java读取

    package com.*.util;
    
    import lombok.extern.slf4j.Slf4j;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    /**
     * @author dhm
     * @desc properties文件读取工具类
     * @date 2017/11/21
     */
    @Slf4j
    public class PropertyUtil {
        private static Properties props;
    
        static {
            loadProps();
        }
    
        synchronized static private void loadProps() {
            log.info("开始加载encrypt.properties文件内容.......");
            props = new Properties();
            InputStream in = null;
            try {
                // in = PropertyUtil.class.getClassLoader().getResourceAsStream("encrypt.properties");
                in = PropertyUtil.class.getResourceAsStream("/encrypt.properties");
                props.load(in);
            } catch (FileNotFoundException e) {
                log.error("encrypt.properties文件未找到");
            } catch (IOException e) {
                log.error("出现IOException");
            } finally {
                try {
                    if (null != in) {
                        in.close();
                    }
                } catch (IOException e) {
                    log.error("encrypt.properties文件流关闭出现异常");
                }
            }
            log.info("加载encrypt.properties文件内容完成...........");
        }
    
        public static String getProperty(String key) {
            if (null == props) {
                loadProps();
            }
            return props.getProperty(key);
        }
    
        public static void main(String[] args) {
            System.out.println(getProperty("name"));
        }
    }

    转载自: https://www.cnblogs.com/zxf330301/p/6184139.html

  • 相关阅读:
    POJ 2778 DNA Sequence(AC自动机+矩阵)
    Challenge & Growth —— 从这里开始
    京东云
    [Done] Codeforces Round #562 (Div. 2) 题解
    HDU 3486 Interviewe
    Codeforces Round #529 (Div. 3) 题解
    Wannafly 挑战赛 19 参考题解
    第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛 题解
    2018年长沙理工大学第十三届程序设计竞赛 题解
    POJ 3017 Cut the Sequence
  • 原文地址:https://www.cnblogs.com/duanhm234/p/7921810.html
Copyright © 2011-2022 走看看