gpt4 book ai didi

java - 我想用 SpringBoot 设置一个 HashMap 并在其他类中获取下一个值

转载 作者:行者123 更新时间:2023-12-04 16:02:36 24 4
gpt4 key购买 nike

我正在使用 Spring Boot,我需要从 Class A 中加载一些值的 HashMap。

然后,我需要从 B 类、C 类等的这个 HashMap 中获取值。

所以我需要一个 HashMap,它首先加载我的值,然后在其他类中使用这个 Map。

谢谢。

最佳答案

我假设你有一个配置类来创建和返回 Spring Beans。

import org.springframework.context.annotation.*;

@Configuration
public class MyConfiguration {

/* Feel free to change the type of key and value in the Map
* from String, String to anything of your choice
*/
@Bean
public Map<String, String> myMap(){
java.util.Map<String, String> map = new java.util.HashMap<String, String>();
map.put("Hello", "world");
return map;
}

/*Your other bean exporting methods*/

}

一旦完成,您就可以像这样将该映射注入(inject)到任何 Spring 组件或服务中

@Component
public class Foo {

@Autowired
private Map<String, String> myMap;

/* You can even put the annotation on a setter */

}

关于java - 我想用 SpringBoot 设置一个 HashMap 并在其他类中获取下一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com