{{format('0')}} {{format('385')}} {{format('992')}}

@ConfigurationProperties注解 静态方法调用无效 [ 技术杂谈 ]

九叶的小窝 文章 正文

As Long As You Love Me.
分享

九叶

{{nature("2022-03-09 15:37:58")}}更新

1. 问题

前一阵子遇到的问题 突然想起来没有写笔记 先记录下来

SpringBoot中使用 @ConfigurationProperties 注入对象 普通方法调用可以 在静态方法中调用的时候读取不到参数

@Component
@ConfigurationProperties(prefix = "constants.ftp")
public class FtpConfig
{
    private static String host;
    private static int port;
    private static String user;
    private static String password;
    private static String uploadPath;
    private static String outputPath;

    public static String getXXX() {
        return xxx;
    }

    public static void setXXX(String xxx) {
        FtpConfig.xxx = xxx;
    }

    ...

}

2. 原因

因为@ConfigurationProperties只会调用非静态的set方法

3. 解决方案

将set方法改为非静态

    public static String getXXX() {
        return xxx;
    }

    public void setXXX(String xxx) {
        FtpConfig.xxx = xxx;
    }

@ConfigurationProperties会根据配置文件的信息调用返回对象的set方法

切记:prefix必须全部为小写,set方法必须为public

参考资料: https://blog.csdn.net/sl1990129/article/details/107064360

评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Minecraft | [1.12.2] 核电工艺模拟器 1.2.25 —— 汉化版

{{nature('2021-12-07 15:29:00')}} {{format('2396')}}人已阅读

TOP 3
SSM搭建Spring单元测试环境

{{nature('2021-01-31 20:01:00')}} {{format('1034')}}人已阅读

TOP 4
dispatcher-servlet.xml文件配置模板

{{nature('2020-12-11 21:17:00')}} {{format('947')}}人已阅读

TOP 5
Windows平台Nacos启动报错无法创建Bean实例

{{nature('2021-04-22 15:16:00')}} {{format('926')}}人已阅读

目录

标签云

注解 SpringBoot

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏