Dear members,
I have a bean propertiesManager for defining the properties of my project, so I want to use this bean in order to get some constant values on other beans. I am setting the lazy-init to false in order to have the garantee this bean will be first instanciated.
Now I want to define a bean that uses such properties defined on the PropertiesManager:Code:<bean id="propertiesManager" class = "com.schinvest.util.PropertiesManager" lazy-init = "false" singleton ="true" > <constructor-arg index="0"> <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location"> <value>classpath:lra.properties</value> </property> </bean> </constructor-arg> <constructor-arg index="1"> <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location"> <value>classpath:lra_testCustom.properties</value> </property> </bean> </constructor-arg> </bean>
on the PositionBatchImpl I have the following declaration that produces a NullPointerException:Code:<bean id="positionBatch" class="com.schinvest.lra.business.batch.position.PositionBatchImpl" singleton = "true" depends-on="propertiesManager"> <property name="propertiesManager"> <ref local="propertiesManager"/> </property> </bean>
I have defined the attribute propertiesManager and its getter/setter method on the coresponding abstract class.Code:public class PositionBatchImpl extends AbstractPositionBatch { private final String XML_EXT = propertiesManager.getProperty(LraPty.XML_EXT); }
If I run under junit my program I get the following initialization errror on the constant definition.
The error comes on the testing class:Code:[testApplicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class [com.schinvest.lra.business.batch.position.PositionBatchImpl]; constructor threw exception; nested exception is java.lang.NullPointerException: null org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'positionBatch' defined in class path resource [testApplicationContext.xml]: Instantiation of bean failedError creating bean with name 'positionBatch' defined in class path resource [testApplicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class [com.schinvest.lra.business.batch.position.PositionBatchImpl]; constructor threw exception; nested exception is java.lang.NullPointerException: null org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'positionBatch' defined in class path resource [testApplicationContext.xml]: Instantiation of bean failed
so, even using lazy-init and depends-on to force that really propertiesManager bean has to be initialized before, that doesn't happen. I am for sure loosing somthing, but I guess it should be a way to do make this kind of dependence.Code:public class TestPositionBatchImpl extends AbstractDependencyInjectionSpringContextTests { protected void onSetUp() throws Exception { this.batch = (PositionBatchImpl) (applicationContext .getBean("positionBatch")); super.onSetUp(); } }
I am planning to convert PropertiesManager into class with static method and attributes, but I don't know if this is a good desing and if it works.
Thanks in advance,
David


Reply With Quote