• 工具类xxxUtil从application.properties中读取参数


    一.原因
    编写一个服务类的工具类,想做成一个灵活的配置,各种唯一code想从配置文件中读取,便有了这个坑。

    二.使用@value获取值为null,
    这是因为这个工具类没有交给spring boot 来管理,导致每次都是new 一个新的,所以每次取出来的值都是nu l l

    三.解决方案
    方法1:

    @Component
    public class MyUtil {
    
        private static String str;
    
        @Value("${zjg.name}")
        public void setStr(String str){
            MyUtil.str = str;
        }
    
    	//使用
        public static void dd(){
            System.out.println(str);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    方法2:

    public class MyUtil {
    
        private final static ResourceBundle resourceBundle = ResourceBundle.getBundle("application");
    
        private  final static  String str = resourceBundle.getString("zjg.name");
    
    	//使用
        public static void dd(){
            System.out.println(str);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注: private final static ResourceBundle resourceBundle = ResourceBundle.getBundle(“application”); application是propertis的前缀,并且是严格匹配的,例如:上述例子,配置参数就必须放在application.properties中,不能放在application-dev.properties中。

    方法3:

    public class MyUtil {
    
        private static Environment env = null;
    
        public static void setEnvironment(Environment env) {
            MyUtil.env = env;
        }
    
        public static String getProperty(String key) {
            return env.getProperty(key);
        }
    
    	//使用
        public static void dd(){
            System.out.println(getProperty("zjg.name"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    配置类:
    @Configuration注解,在这个类中将Environment注入PropertiesUtil中

    @Configuration
    public class PropertiesConfig {
    
        @Resource
        private Environment env;
    
        @PostConstruct
        public void setProperties() {
            MyUtil.setEnvironment(env);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    方法4:

    ublic class ResourceUtil {
    
        static Properties properties = null;
    
        static{
            properties=new Properties();
            InputStream in= ResourceUtil.class.getClassLoader().getResourceAsStream("application.properties");
            try {
                properties.load(in);
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static String getValue(String key){
            return properties.getProperty(key);
        }
    
        public static Properties getProperties(){
            return properties;
        }
    
    	//使用
        public static void dd(){
           System.out.println(ResourceUtil.getValue("zjg.name"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    OpenAI视频生成Sora技术简析
    zabbix基本介绍 安装部署 页面访问
    23种设计模式中之中介者模式(Mediator Pattern)
    MySQL 一键安装 (支持8.0.16-8.0.30)
    POJ3275 Ranking the Cows题解
    餐饮新零售增长:真功夫集团如何组织各事业部实现规模化增长运营能力
    二刷 K8s 源码 - workqueue 的所有细节
    Vue三种3D动态词云实现
    C++ Reference: Standard C++ Library reference: C Library: cstdio: rewind
    【C++】STL07 关联容器-set
  • 原文地址:https://blog.csdn.net/u014365523/article/details/134272712