I have recently joined a new project and was surprised to see the bean management in the project. Though they use Spring for dependency injections , once the beans have been instantiated they create another map where they maintain a mapping :
class type => bean reference
In the application whenever a bean is needed, they do map.get(class type) rather than doing applicationcontext.getBean().
The code comments say that this was done as spring's applicationcontext.getBean() creates a garbage data of 150KB per call. I looked at the code for getBean() method and couldn't understand why did the author think garbage will be created.
This is code that creates the map :
private static Map<Class, Object> beans = new HashMap<Class, Object>();
public <T> T getBean(Class<T> clazz)
Object object = beans.get(clazz);
if(object == null)
// Caching bean as spring get bean creates approx 150KB of garbage per call.
object = appContext.getBean(clazz);
This didn't make any sense to me hence requesting the opinion of the experts on this matter.