springboot中使用@Value获取yml中的属性值注入静态变量为空

2022-06-30 13:16:31

1.application.yml中配置内容如下:

box:
  host: 192.168.1.5
  port: 30000

2.通过@Value获取值:

    private static String host;

    private static int port;

    public static String getHost() {
        return host;
    }
    @Value("${box.host}")
    public  void setHost(String host) {
        AudioBroadcast.host = host;
    }

    public static int getPort() {
        return port;
    }
    @Value("${box.port}")
    public  void setPort(int port) {
        AudioBroadcast.port = port;
    }

心得:使用注解的方式,不过注解写在非static的方法上(Spring的注解不支持静态的变量和方法)。

  • 作者:观望过往
  • 原文链接:https://blog.csdn.net/qq_44608856/article/details/110131218
    更新时间:2022-06-30 13:16:31