I'm using a FactoryBean to instantiate an object, however, Spring invokes the getObject() method before any properties are set on the factorybean. As far as I know this is not normal behaviour. I must be missing something, but I can't seem to find what it is. The created object is used to run a task every hour.
Here is the Spring bean configuration:
Here is part of the factory bean source:Code:<bean id="importUpdates" class="nl....domain.logic.spring.ImportUpdatesFactoryBean"> <property name="xmlConfigAsResource"><value>/.../data/orderupdate.xml</value></property> <property name="storeName"><value>statusUpdate</value></property> <property name="dataDirectory"><value>data</value></property> <property name="archiveDirectory"><value>data/archive</value></property> <property name="orderDao"><ref local="orderDAO"/></property> <property name="organisationDao"><ref local="organisationDAO"/></property> </bean> <bean id="updateTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject"><ref bean="importUpdates"/></property> <property name="targetMethod"><value>update</value></property> </bean> ..
And here is part of the stacktrace, where things go wrong.Code:private URL _xmlConfigLocation; private URI _dataDirectory; private File _archiveDirectory; private String _storeName; private OrderDAO _orderDao; private OrganisationDAO _organisationDao; /** * Sets the XML configuration store location as an URL. */ public void setXmlConfigAsURL(URL url) { if (DEBUG) { LOG.debug("setXmlConfigAsURL(" + url + ")"); } if (url == null) { throw new NullPointerException("url"); } _xmlConfigLocation = url; } /** * Sets the XML configuration store location as an resource. */ public void setXmlConfigAsResource(String resource) { if (DEBUG) { LOG.debug("setXmlConfigAsResource(" + resource + ")"); } if (resource == null) { throw new NullPointerException("resource"); } _xmlConfigLocation = getClass().getResource(resource); if (_xmlConfigLocation == null) { throw new IllegalArgumentException("Resource " + resource + " not found"); } } // ... /** * getObject is an overwritten/implemented * method of a parent or interface. * * @see org.springframework.beans.factory.FactoryBean#getObject() */ public Object getObject() throws Exception { if (DEBUG) { LOG.debug("getObject()"); } if (_dataDirectory == null) { throw new IllegalStateException("Could not instantiate Updater: No data directory"); } if (_archiveDirectory == null) { throw new IllegalStateException("Could not instantiate Updater: No archive directory"); } if (_xmlConfigLocation == null) { throw new IllegalStateException("Could not instantiate Updater: No XML configuration"); } if (_storeName == null) { throw new IllegalStateException("Could not instantiate Updater: No store name"); } if (_organisationDao == null) { throw new IllegalStateException("Could not instantiate Updater: " + "No organisation DAO set"); } if (_orderDao == null) { throw new IllegalStateException("Could not instantiate Updater: No order DAO set"); } XmlTableStoreFactory tsf = new XmlTableStoreFactory(_xmlConfigLocation, _dataDirectory); TableStore ts = tsf.getTableStore(_storeName); ImportUpdates iu = new ImportUpdates(ts, _archiveDirectory); iu.setOrderDAO(_orderDao); iu.setOrganisationDAO(_organisationDao); return iu; } /** * getObjectType is an overwritten/implemented * method of a parent or interface. * * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ public Class getObjectType() { return ImportUpdates.class; } /** * isSingleton is an overwritten/implemented * method of a parent or interface. * * @see org.springframework.beans.factory.FactoryBean#isSingleton() */ public boolean isSingleton() { return true; }
So can anyone help me out? What am I doing wrong? How can I make it work.Code:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'importUpdates': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Could not instantiate Updater: No data directory java.lang.IllegalStateException: Could not instantiate Updater: No data directory at nl.....domain.logic.spring.ImportUpdatesFactoryBean.getObject(ImportUpdatesFactoryBean.java:144) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForSharedInstance(AbstractBeanFactory.java:794) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:176) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:105) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:920) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:731) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:340)


Reply With Quote