我常用的两种:
1.注解获取

#测试读取配置文件
is_properties_message="hello world !"

通过编写controller测试获取结果

package com.example.produce.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @package: com.example.produce.controller
 * @project: rabbitmq
 * @author: ysten_song
 * @date : 2021/9/13 15:04
 */
@RestController
public class TestController {
    @Value(value = "${is_properties_message}")
    private String str ;
    
    @RequestMapping("/aa")
    public String getMessage(){
        return str ;
    }
}

访问结果

2.编写方法获取
1.编写读取配置文件方法

package com.example.produce.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
 * @package: com.example.produce.utils
 * @project: rabbitmq
 * @author: ysten_song
 * @date : 2021/9/13 14:14
 */
@Slf4j
public class ReadProperty {
        private static ReadProperty dbconfig = new ReadProperty();
        private static Properties props = new Properties();;
        public static ReadProperty getInstance() {
            return dbconfig;
        }
        private void loadProperties() {
            InputStreamReader in = null;
            try {
                in = new InputStreamReader(ReadProperty.class.getResourceAsStream("/application.properties"), "UTF-8");
                props.load(in);
            } catch (Exception e) {
                log.error("load application  configuration exception", e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        log.error("IO exception", e);
                    }
                }
            }
        }
        public static void refresh() {
            ReadProperty.getInstance().loadProperties();
        }
        public static String getProperty(String propName) {
            if (props.isEmpty()) {
                ReadProperty.getInstance().loadProperties();
            }
            return props.getProperty(propName);
        }
        public static String get(String propName) {
            return getProperty(propName);
        }
    }

2、测试是否能够读取信息

package com.example.produce.test;
import com.example.produce.utils.ReadProperty;
/**
 * @package: com.example.produce.test
 * @project: rabbitmq
 * @author: ysten_song
 * @date : 2021/9/13 14:18
 */
public class ReadPropertyTest {
    private static final String is_property_message= "is_properties_message";
    public static void main(String[] args) {
        String property = ReadProperty.getProperty(is_property_message);
        System.out.println(property);
    }
}

3、结果

简单的方法

//通过java.util包下的ResourceBundle资源绑定器进行获取
        ResourceBundle application = ResourceBundle.getBundle("application");
        String message = application.getString("is_properties_message");
        
//通过流获取
        InputStream reader = Thread
                .currentThread()
                .getContextClassLoader()
                .getResourceAsStream("application.properties");
        //创建属性类对象Map
        Properties properties = new Properties();
        //加载
        properties.load(reader);
        //关闭流
        reader.close();
        String message = properties.getProperty("is_properties_message");

------------恢复内容结束------------

posted on 2021-09-13 15:58  剑舞红颜笑i  阅读(148)  评论(0)    收藏  举报