搜索
首页Java从应用程序 yml 加载对象列表 Java Spring Boot

php小编西瓜带你深入探讨Java中应用程序yml如何加载对象列表的问题。在Java Spring Boot中,通过合理配置yml文件,可以实现对对象列表的加载,并在应用程序中灵活应用。接下来,让我们一起来掌握这一技巧,提升在Spring Boot开发中的实用性和效率。

问题内容

我有这个配置类:

@configuration
@configurationproperties(prefix = "house")
public class projectconfig{

    private list<housetemplate> templates;

    // getters and setters
}

housetemplate 类:

public class housetemplate{

    private string id;
    private string description;

    // getters and setters
}

这是我的应用程序-test.yml

house:
  templates:
    -
      id: "colonial"
      description: "colonial house template"
    -
      id: "cottage"
      description: "cottage house template"
    -
      id: "apartment"
      description: "apartment house template"


examplestring: hello

在我的测试类中,我有以下内容:

@runwith(springrunner.class)
@enableconfigurationproperties(value = projectconfig.class)
@testpropertysource("classpath:application-test.yml")
public class yamlapplicationcontextloadingspec {

    @value("${examplestring}")
    string example;

    @autowired
    projectconfig projectconfig;


    @test
    public void exampleshouldcontainhello(){
        assertthat(example).isequaltoignoringcase("hello");
    }
   
    @test
    public void appcontextcontainshousetemplates(){
        list<housetemplate> housetemplates = projectconfig.gettemplates();
        assertthat(housetemplates).isnotnull();
    }
}

关于 examplestring 的第一个测试通过,而第二个测试没有通过。为什么无法将yml映射到housetemplate列表中?

编辑

<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>

<artifactId>spring-boot</artifactId>
<version>1.5.1.RELEASE</version>

<artifactId>junit</artifactId>
<version>4.12</version>

我知道它们真的很旧,我很想升级,但我不能……这就是我必须处理的。

解决方法

使用@configurationproperties从yml中读取后,可以将其注册为bean,然后可以通过applicationcontext获取。

@configuration
@configurationproperties(prefix = "house")
public class testconfig {

    private list<housetemplate> templates;

    @bean
    public list<housetemplate> templates() {
        return templates;
    }

    // getter and setter
}

@restcontroller
@requestmapping("/api/test")
public class testcontroller {

    @autowired
    private applicationcontext applicationcontext;

    @getmapping("get")
    @suppresswarnings("unchecked")
    public list<housetemplate> get() {
        return (list<housetemplate>) applicationcontext.getbean("templates");
    }
}

如果你不想注册为bean,也可以将变量声明为静态。

这是一个示例。

@Configuration
@ConfigurationProperties(prefix = "House")
public class TestConfig {

    private static List<HouseTemplate> templates;

    public static List<HouseTemplate> get() {
        return templates;
    }

    public List<HouseTemplate> getTemplates() {
        return templates;
    }

    public void setTemplates(List<HouseTemplate> templates) {
        TestConfig.templates = templates;
    }
}

@RestController
@RequestMapping("/api/test")
public class TestController {

    @GetMapping("get")
    public List<HouseTemplate> get() {
        return TestConfig.get();
    }
}

以上是从应用程序 yml 加载对象列表 Java Spring Boot的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!