@ConfigurationProperties与静态加载配置文件时取值为null

 先贴上错误代码: 

@Component
@ConfigurationProperties(prefix = "toss")
public class TossConfig {

    /**文件上传路径*/
    private static String profile;

    public static String getProfile() {
        return profile;
    }

    public static void setProfile(String profile) {
        TossConfig.profile = profile;
    }

    public static String getDownloadPath(){
        return profile + "download/";
    }
}

项目运行后发现 profile取值是 null;
这是因为@ConfigurationProperties只会调用 非静态的set方法,所以稍做改动,set方法都换成非静态的,贴下正确的代码:

@Component
@ConfigurationProperties(prefix = "toss")
public class TossConfig {

    /**文件上传路径*/
    private static String profile;

    public static String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        TossConfig.profile = profile;
    }

    public static String getDownloadPath(){
        return profile + "download/";
    }
}

 

你可能感兴趣的:(spring,boot)