• SpringBoot获取不到application.properties的值


    导读

      最近在搭建消息网关服务,因为里面用到了设计模式,启动的时候,没有被Spring管理到,使用@Value(${})迟迟获取不到application.properties里的值,然后手写一个工具类,其他地方调用的时候,只需要从工具类中获取key即可。

    工具类

    package com.ybchen.smsgateway.config;
    
    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.IOException;
    import java.util.Properties;
    
    /**
     * @ClassName:SystemConfig
     * @Description:系统配置类
     * @Author:chenyb
     * @Date:2020/12/2 4:11 下午
     * @Versiion:1.0
     */
    @Component
    public class SystemConfig {
        private static Properties props ;
        public SystemConfig(){
            try {
                Resource resource = new ClassPathResource("/application.properties"); //读取那个配置文件
                props = PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static String getProperty(String key){
    
            return props == null ? null :  props.getProperty(key);
    
        }
        public static String getProperty(String key,String defaultValue){
    
            return props == null ? null : props.getProperty(key, defaultValue);
    
        }
        public static Properties getProperties(){
            return props;
        }
    }

    调用

    SystemConfig.getProperty("wx.appID")

  • 相关阅读:
    Linux下卸载Oracle 11g
    Oracle
    Oracle 数据库启动与关闭
    1-centos7安装oracle 11gR2
    0-windows7硬盘安装centos7
    查重 查重复记录 删除重复记录
    用户情景快速指南
    智能led灯具HMI(无线终端参数设置界面)
    LBDP-Z APP在线升级指南
    智能led灯具HMI(用户模块及管理员模块)操作说明
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/14074494.html
Copyright © 2020-2023  润新知