首页 > Java > 正文

从应用程序 yml 加载对象列表 Java Spring Boot

WBOY
发布: 2024-02-22 13:16:05
转载
1341人浏览过

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
登录后复制

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

立即学习Java免费学习笔记(深入)”;

@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中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号