SpringBoot读取properties文件的方式

一:ConfigurationProperties

application.properties

com.aaron.title=My first Page中文
com.aaron.description=This is my first page!中文
#Map
com.aaron.login[username]=jack
com.aaron.login[password]=Admin@123
com.aaron.login[callback]=http://127.0.0.1/hello
#List
com.aaron.urls[0]=http://127.0.0.1/hello
com.aaron.urls[1]=http://127.0.0.1/getUser
com.aaron.urls[2]=http://127.0.0.1/getName
com.aaron.urls[3]=http://127.0.0.1/getPage

import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * SpringBoot读取properties文件的方式一:ConfigurationProperties
 * @author aaron
 */
@Component
@ConfigurationProperties(prefix = "com.aaron")
//@PropertySource(value = "config.properties")  //默认application.properties
public class PropertiesConfig {

    private String title;
    
    private String description;
    
    private Map<String, String> login = new HashMap<String, String>();
    
    private List<String> urls = new ArrayList<String>();

public String getDescription() {
        try {
            return new String(description.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
        }
        return description;
    }
//其他get/set略 }
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aaron.springBootDemo.domain.PropertiesConfig;

@RestController
public class HelloWorldController {
    
    @Autowired
    private PropertiesConfig propertiesConfig;
    
    @GetMapping("/config")
    public Map<String, Object> configurationProperties() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", propertiesConfig.getTitle());
        map.put("description", propertiesConfig.getDescription());
        map.put("login", propertiesConfig.getLogin());
        map.put("urls", propertiesConfig.getUrls());
        return map;
    }
}

二:Value

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * SpringBoot读取properties文件的方式二:Value
 * @author aaron
 */
@Component
public class PropertiesBean {
    
    @Value("${com.aaron.title}")
    private String title;
    
    @Value("${com.aaron.description}")
    private String description;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aaron.springBootDemo.domain.PropertiesBean;

@RestController
public class HelloWorldController {
    
    @Autowired
    private PropertiesBean propertiesBean;
    
    @GetMapping("/getPage")
    public PropertiesBean getPage() {
        return propertiesBean;
    }
}

三:Environment

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    
    @Autowired
    private Environment env;
    
/**
     * SpringBoot读取properties文件的方式三:Environment
     * @return
     */
    @RequestMapping("/env")
    public Map<String, Object> env() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", env.getProperty("com.aaron.title"));
        map.put("description", env.getProperty("com.aaron.description"));
        return map;
    }
}

四:Listener

myconfig.properties

com.aaron.my.title=My Config title标题
com.aaron.my.description=This is my config!描述

import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesListenerUtil {

    private static Map<String, String> propertiesMap = new HashMap<String, String>();
    
    public static void loadAllProperties(String propertyFileName) {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
            propertiesMap.clear();
            for(Object key : properties.keySet()) {
                String keyStr = key.toString();
                propertiesMap.put(keyStr, properties.getProperty(keyStr));
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    
    public static String getProperty(String name) {
        return propertiesMap.get(name);
    }
    
    public static Map<String, String> getAllProperty() {
        return propertiesMap;
    }
}
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;

import com.aaron.springBootDemo.util.PropertiesListenerUtil;

public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {
    
    private String propertyFileName;
    
    public PropertiesListener(String propertyFileName) {
        this.propertyFileName = propertyFileName;
    }

    @Override
    public void onApplicationEvent(ApplicationStartedEvent arg0) {
        PropertiesListenerUtil.loadAllProperties(propertyFileName);
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.aaron.springBootDemo.listener.PropertiesListener;

@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(SpringBootDemoApplication.class, args);
        SpringApplication app = new SpringApplication(SpringBootDemoApplication.class);
        /**
         * SpringBoot读取properties文件的方式四:Listener
         */
        app.addListeners(new PropertiesListener("myconfig.properties"));
        app.run(args);
    }
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aaron.springBootDemo.util.PropertiesListenerUtil;

@RestController
public class HelloWorldController {
/**
     * SpringBoot读取properties文件的方式四:Listener
     * @return
     */
    @RequestMapping("/listener")
    public Map<String, String> listener() {
        Map<String, String> map = new HashMap<String, String>();
        map.putAll(PropertiesListenerUtil.getAllProperty());
        return map;
    }
}

 

posted @ 2018-04-23 16:21  AaronCnblogs  阅读(127)  评论(0)    收藏  举报