I'm working on implementing hierarchical contexts, and I've run into a situation I assumed would be handled... I've got a prototype bean definition in my parent context which has a property of type Counter (for example). In my child context (which has its parent set to the parent context) I define the Counter bean. Now, when I do a getBean() on the BeanFactory of the child context for the prototype bean defined in the parent context, I would expect that its dependencies from the child context would be initialized, but they're not.
I think it's a problem with AbstractBeanFactory in the getBean(String name, Class requiredType, Object[] args) method where it does this:
The problem is that if it's defined in this context, it goes on to do some other initialization, whereas if it's defined in the parent context it just returns. I think if the parent context's bean definition is a prototype, it should go ahead and do some more initialization / autowiring to give it all of its dependencies.Code:try { mergedBeanDefinition = getMergedBeanDefinition(beanName, false); } catch (NoSuchBeanDefinitionException ex) { // Not found -> check parent. if (this.parentBeanFactory instanceof AbstractBeanFactory) { // Delegation to parent with args only possible for AbstractBeanFactory. return ((AbstractBeanFactory) this.parentBeanFactory).getBean(name, requiredType, args); } else if (this.parentBeanFactory != null && args == null) { // No args -> delegate to standard getBean method. return this.parentBeanFactory.getBean(name, requiredType); } throw ex; }
I don't want to have to define the beans which use the child objects for each and every child context when they're the same across all of them (more for performance and memory use than anything else). [/code]


Reply With Quote