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

  • 相关阅读:
    Attribute在.NET编程中的应用
    Attribute应用权限验证
    Enterprise Library: Configuration Application Block
    App_Themes
    C# DNN 地址
    c++函数参数类型引用、指针、值
    四川新华社 关于IT诗人代腾飞 新书《赢道:成功创业者的28条戒律》新闻报道
    30岁之前创业想成功必看
    大师指点—为人处世秘诀之快乐成功之道!
    四川经济日报 关于代腾飞 新书《赢道:成功创业者的28条戒律》新闻报道
  • 原文地址:https://www.cnblogs.com/duanhm234/p/7921810.html
Copyright © 2011-2022 走看看