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

posted @ 2017-11-29 17:36  舟舟舟舟舟  阅读(798)  评论(0编辑  收藏  举报