spring引导从配置文件加载列表会返回空列表

fwzugrvs  于 2021-07-12  发布在  Java
关注(0)|答案(4)|浏览(297)

我刚刚用spring初始化器创建了一个非常基本的spring启动应用程序,正在进行尝试。我想从yaml配置文件加载一个列表,但它总是返回空的。
我有一个自定义配置类

@ConfigurationProperties("example-unit")
@EnableConfigurationProperties
public class ConfigurationUnit {

    public List<String> confiList = new ArrayList<>();

    public List<String> getConfiList() {
        return this.confiList;
    }

}

我的主课是这样的

@SpringBootApplication
public class DemoApplication {

    static ConfigurationUnit configurationUnit = new ConfigurationUnit();

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

        List<String> hello = configurationUnit.getConfiList();

        System.out.print("");
    }

}

我已将application.yaml放入resources文件夹。

example-unit:
  - string1
  - string2
  - hello22

我在这里和网上搜索,但无法找出问题所在,我所做的任何更改都没有帮助。我知道我一定做错了什么。

f4t66c6m

f4t66c6m1#

这种说法是错误的 static ConfigurationUnit configurationUnit = new ConfigurationUnit(); 不应创建对象
spring只将属性注入到由应用程序上下文处理的bean中,spring创建带有注解的类bean @ Configuration 配置单元

@Configuration
@ConfigurationProperties("example-unit")
public class ConfigurationUnit {

public List<String> confiList;

public List<String> getConfiList() {
    return this.confiList;
    }

 }

spring boot main中的demoapplication从applicationcontext获取bean,并从中获取list对象

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {

    ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
     ConfigurationUnit unit = context.getBean("configurationUnit"):

    System.out.print(unit. getConfiList());
   }

}
fzwojiic

fzwojiic2#

将列表置于prefix.property下。对你来说 example-unit.confi-list: . 通常为您的财产提供一个设置器: setConfiList(List<String> strings) . 但由于您已经将它初始化为空数组列表,所以这个setter已经过时了。还有一个建议是向应用程序类添加启用注解:
应用 班级应该有 @启用配置属性 注解

drkbr07n

drkbr07n3#

下面是关于springbboot配置绑定工作原理的参考资料。
特别针对您的问题,这是一个实现您目标的应用程序示例:
应用程序.yml

example-unit:
  confiList:
    - string1
    - string2
    - hello22

来源

@SpringBootApplication
@EnableConfigurationProperties(ConfigurationUnit.class)
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        ConfigurationUnit configurationUnit = context.getBean(ConfigurationUnit.class);
        System.out.println(configurationUnit.getConfiList());
    }

}

@ConfigurationProperties("example-unit")
public class ConfigurationUnit {

    public List<String> confiList = new ArrayList<>();

    public List<String> getConfiList() {
        return this.confiList;
    }

}
mccptt67

mccptt674#

举个例子:
应用程序.yml:

example-unit: string1,string2,hello22

配置单元类:

@Component
@PropertySource(value="classpath:application.yml")
public class ConfigurationUnit {

    @Value("#{'${example-unit}'.split(',')}")
    private List<String> confiList;

    public List<String> getConfiList() {
        return confiList;
    }
}

demofileloadapplication.class:类:

@SpringBootApplication
public class DemoFileLoadApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoFileLoadApplication.class, args);
        ConfigurationUnit configurationUnit = context.getBean(ConfigurationUnit.class);
        System.out.println(configurationUnit.getConfiList());
    }
}

输出:
[string1,string2,你好22]

相关问题