Sometimes I have some problems with the order of the initializing of beans which end in strange "null errors" or even NPEs. The reasons seems to be that beans are made available before they are completely initialized. This happened with Spring 1.2.x and now with 2.0 RC 1 as well.
Example: In a really huge error message I can somewhere read the following
The setup looks like the following:Code:Error creating bean with name 'corbaConnectionFactory': postProcessAfterInitialization method of BeanPostProcessor [org.springframework.context.support.ApplicationContextAwareProcessor@787c16] returned null for bean [null] with name [corbaConnectionFactory] ... org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:958) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectFromFactoryBean(AbstractBeanFactory.java:996)
Debugging the code I found out that no properties had been set on the LocalConnectionFactoryBean and afterPropertiesSet() never had been called. And it's not understandable why.Code:<bean id="corbaManagedConnectionFactory" init-method="init" class="com.mycompany.resource.corba.jca.spi.CorbaManagedConnectionFactory"> </bean> <bean id="corbaConnectionFactory" class="org.springframework.jca.support.LocalConnectionFactoryBean"> <property name="managedConnectionFactory" ref="corbaManagedConnectionFactory"/> <property name="connectionManager" ref="connectionManager"/> </bean>
(A better error message could maybe provided by checking for null in LocalConnectionFactoryBean.getObject().)
Another example I have with Jencks being involved. Jencks' GeronimoTransactionManagerFactoryBean is ApplicationContextAware and stores the ApplicationContext in a field in setApplicationContext(..). But it can happen that when getObject() is called, a NPE is thrown due to not yet set ApplicationContext. Again I don't know why. Even stranger: This error depends on the order of the passed config locations when setting of the ApplicationContext.
(Actually Jencks is involved in example 1 as well. I have 3 context locations, 5 combinations resulting in the NPE, 1 in the "null error" from example 1. My testcase worked with the 1 combination with Spring 1.2.x, but failed for the other combinations with the NPE as well.)
Any ideas how this can happen?
Jörg


Reply With Quote