Lui*_*sao 3 java spring hashmap spring-boot
我正在使用 Spring Boot,我需要加载一个包含 A 类中的一些值的 HashMap。
然后,我需要从B类、C类等的这个HashMap中获取值。
所以我需要一个 HashMap,它首先加载我的值,然后在其他类中使用这个 Map。
谢谢。
我假设您有一个创建并返回 Spring Bean 的 Configuration 类。
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*/
}
Run Code Online (Sandbox Code Playgroud)
完成后,您可以将此映射注入到任何 Spring 组件或服务中,如下所示
@Component
public class Foo {
@Autowired
private Map<String, String> myMap;
/* You can even put the annotation on a setter */
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23455 次 |
| 最近记录: |