关于Spring boot重构Spring cloud项目的自定义Properties为空问题

Spring Cloud读取yml配置null

最近重构项目时将原项目Spring boot重构成Spring cloud时,发现我在Spring boot项目里写的一个获取yml配置的Properties类,取值为null。经过多次调试发现这个异常问题。并且暂时没有解决。请各位帮忙分析一下。后期如果找到办法解决将更新此博客。

这里贴一下代码片段 Spring boot项目的yml:

  facelog:
    host: xxxxxx
    port: xxxxx

这里是自定义属性类

@Component
@PropertySource(value = "classpath:application.yml")
@ConfigurationProperties(prefix = "fdevmgr.facelog")
public class FacelogProperties {
    private static  Integer facelogPort;


    private static String facelogHost;

    private static String rootPwd;

    public Integer getFacelogPort() {
        return facelogPort;
    }

    @Value("${port}")
    public void setFacelogProt(Integer facelogProt) {
        FacelogProperties.facelogPort = facelogProt;
    }

    public String getFacelogHost() {
        return facelogHost;
    }

    @Value("${host}")
    public void setFacelogHost(String facelogHost) {
        FacelogProperties.facelogHost = facelogHost;
    }



}

在这里能看到这是一个最基础简单的获取自定义属性类,当然我这是获取静态的属性并且是public的,是因为我这边的代码业务才做的改动,表示只要在@Value注释上的set方法不是静态就可以了,相关问题可以参考一下资料。

下面是Spring Cloud项目的运行流程

启动项目初始化加载Properties类

在这里插入图片描述

可以看到这里是加载进来了值,并且继续调试中,然后再调用属性的时候发现变成了null

在这里插入图片描述

以上就是我发现的情况。如果有了解的人,请评论或者私信我。谢谢

发布了1 篇原创文章 · 获赞 0 · 访问量 83

猜你喜欢

转载自blog.csdn.net/qq_33167006/article/details/104677840