四、SpringBoot 读取Properties

文章目录


#LoadProperties
##Util
本文中提到的Properties都放在resource目录下
下面是加载工具类:

package com.ifreedom.beauty.util;

import com.sun.deploy.util.Property;
import com.sun.tools.internal.ws.wsdl.document.jaxws.Exception;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

/**
 * Created by andy.wu
 */


public class PropertyUtil {
    private static Map<String, String> propertiesMap = new HashMap<>();
    // Default as in PropertyPlaceholderConfigurer


    public static void processProperties( Properties props) throws BeansException {

        propertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();

            try {
                //PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
                propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }catch (java.lang.Exception e){
                e.printStackTrace();
            };
        }
        System.out.println(propertiesMap);
    }
    public static void loadAllProperties(){
        try {

            Properties properties = PropertiesLoaderUtils.loadAllProperties("String.properties");
            processProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String name) {
        return propertiesMap.get(name).toString();
    }
}

##usage
使用的方法是.在SpringApplication启动时。addApplicationListener,在回掉里面写上PropertyUtil.loadAllProperties(PropertiesName);
##example
application for eg:

@SpringBootApplication
public class BeautyApplication {
    public static void main(String[] args) {

        SpringApplication application = new SpringApplication(BeautyApplication.class);
        application.addListeners(new ApplicationStartUpListener());
        application.run(args);
    }
}

listener for eg:

public class ApplicationStartUpListener implements ApplicationListener<ApplicationStartedEvent> {


    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        PropertyUtil.loadAllProperties();
        System.out.println("ApplicationStartUpListener EXEC");
    }
}

github:https://github.com/HumorSmith/SprintBoot.git

博主开发的第三方CSDN客户端.体验很棒哦,快来体验下载吧
在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页