Issue
I create a map of Map objects for autowiring in a class. The creation of the Map loads fine. However, when I autowire the map in the class the keys change to lower case values of the object names. I print the keys just after the creation of the bean and the keys are what I expect. However, the key values become something else once they are autowired. The objects to be loaded for example are org.bcn.sys.BasicDataobject and the key becomes basicDataObject. Very weird. Can someone tell me what's going on? The properties are from a Properties file. For example,
dat1=org.bcn.sys.BasicDataobject becomes basicData, key is not dat1 but something like basicDataobject!!! For example
@Bean
public Map<String,DataObject> dataObjects(){
Map<String,DataObject> m=new HashMap<String,DataObject>();
for(Map.EntrySet<String,String> e: e.entrySet()){
m.put(e.getKey(), loadDataObject(e.getValue());
}
//Prints keys correctly
LOG.info("m contains "+m)
}
@Component
@Import(AppConf.class)
class DataObjectProcessor{
@Autowired
private Map<String,DataObject> dataObjects;
// Keys are lower case names of the DataObjects!!!!
}
Solution
Spring is using the Component names as the keys, I have no idea why it is doing that! So for example it is using dataObject as the key when the class is annotated with @Component witout the value attribute. When I applied the value attribute to the @Component annotation using @Component(value="dat1"), Spring started using dat1 as the key!!! I am surprised it is replacing my keys since the Component is the value portion of the map. I hope this helps someone in a similar situation
//Uses dataObject as key
@Component
public class DataObject implement BasicTypeable{
}
//Uses dat1 as key
@Component(value="dat1")
public class DataObject implement BasicTypeable{
}
Answered By - BreenDeen