• 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

  • 相关阅读:
    【学习】CodeUi
    【C#】权限修饰符
    链表_初步认识
    关于mysql8.0及以上版本连接navicat时候报错(密码加密方式需要修改)
    docker 运行mysql最新版本用navicat连接报错:1251
    spring5的基本组成(6个模块)
    关于java中设计原则总结(7)
    java中String中的endsWith()方法
    mysql中基本的语句
    mysql中索引类型
  • 原文地址:https://www.cnblogs.com/duanhm234/p/7921810.html
Copyright © 2020-2023  润新知