zoukankan      html  css  js  c++  java
  • 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")

  • 相关阅读:
    3.30 DOM操作
    3.29 js例题
    3.28 函数
    3.27 数组例题
    Web 条件查询、分页查
    web页面增、删、改
    JDBC事务、下拉框
    JSTL、断点、JavaEE、DBUTils连接池
    jsp、el表达式
    Session技术 、jsp页面
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/14074494.html
Copyright © 2011-2022 走看看