springboot中引用配置文件中的参数

 首先可以看到这是做微信登陆时候的配置,一般不会写死都是通过配置文件获取,所以,记载配置文件中

 那么怎么引用呢:

 可以看到直接注入的方式就可以引用了,所以看下面:

进行页面跳转,并且带有参数的,

使用modelandview进行,或者采用返回一个字符串的方式进行

 

 还有中方式就是,丢在resource下面,然后进行读取,需要工具类:

import java.io.*;
import java.net.URI;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;



public class PropertiesUtil {

    private static Properties props = null;
    private static URI uri;
    private static String fileName = "/weixinconnectconfig.properties";
    
    private static InputStream in = null;

    static {
        try {
            props = new Properties();
            InputStream fis = PropertiesUtil.class.getResourceAsStream(fileName);
            props.load(fis);
            uri = PropertiesUtil.class.getResource(fileName).toURI();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取某个属性
     */
    public static String getProperty(String key) {
        try {
            props.load(PropertiesUtil.class.getResourceAsStream(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props.getProperty(key);
    }

    /**
     * 获取所有属性,返回一个map,不常用 可以试试props.putAll(t)
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, String> getAllProperty() {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration enu = props.propertyNames();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = props.getProperty(key);
            map.put(key, value);
        }
        return map;
    }

    /**
     * 在控制台上打印出所有属性,调试时用。
     */
    public static void printProperties() {
        props.list(System.out);
    }

    /**
     * 写入properties信息
     */
    public static void writeProperties(String key, String value) {
        try {
            OutputStream fos = new FileOutputStream(new File(uri));
            props.setProperty(key, value);
            // 将此 Properties 表中的属性列表(键和元素对)写入输出流
            props.store(fos, "『comments』Update key:" + key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 取默认key的value
     * */
    public static String getValue(String key){
        String value = null;
        props = new Properties();
        in = PropertiesUtil.class.getResourceAsStream(fileName);
        try {
            props.load(in);
        } catch (IOException e) {
//            e.printStackTrace();
        }
        value = (String) props.get(key);
        return value;
    }

}
原文地址:https://www.cnblogs.com/xiufengchen/p/11754513.html