天天看點

四、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用戶端.體驗很棒哦,快來體驗下載下傳吧
四、SpringBoot 讀取Properties