今天在写程序的时候写傻了,获取配置的时候,对于Map<String,String>类型的数据也使用了@Bean 自动注入了一个Map<String,String>类型的Bean。
@Bean
@Primary
public Map<String,String> getConfigs(){
Map<String,Object> configs=new HashMap<>();
configs.put("server","localhost");
configs.put("port", "8080");
configs.put("max","5");
configs.put("min","1");
return configs;
}
@Bean
public TestObject testObject(@Qualifier(value = "getConfig") Map<String ,Object> configs){
TestObject testObject=new TestObject ();
Object config=configs.get("min");
return testObject;
}
最后的testObject中取到的是两层map接口的数据
所以,对与简答的数据类型,不也用@Bean这么麻烦的东西取自动注入,直接使用就好。
最后改成
@Bean
@Primary
public TestObject testObject(){
Map<String,Object> configs=new HashMap<>();
configs.put("server","localhost");
configs.put("port", "8080");
configs.put("max","5");
configs.put("min","1");
TestObject testObject=new TestObject ();
//do your work
return testObject;
}