Announcement Announcement Module
Collapse
No announcement yet.
@Configurable and bean creation exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configurable and bean creation exception

    Hi All

    This is probably a post that has been posted many times before so apologies! I am trying to do some tests using @Configurable and have got the sample code to work. If I add a service to my test bean and annotate it with @configurable and then create a new instance of the bean the service is injected properly. My problem comes when I try to add a dao to the service class and then set the service to the annotated bean. I get the following exception:


    Code:
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@d7b222: defining beans [org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect,org.springframework.context.config.internalAutowiredAnnotationProcessor,org.springframework.context.config.internalRequiredAnnotationProcessor,org.springframework.context.weaving.AspectJWeavingEnabler,loadTimeWeaver,dataSource,sessionFactory,hibernateTemplate,testDao,testService,testBean,propertyConfigurer]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amin/atconfigurable/test/resource/context.xml]: Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectcom.amin.atconfigurable.test.bean.TestBean
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1178)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:407)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:255)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:252)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:689)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:358)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
    	at com.amin.atconfigurable.test.client.Test.main(Test.java:11)
    Caused by: org.hibernate.InstantiationException: could not instantiate test objectcom.amin.atconfigurable.test.bean.TestBean
    	at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:25)
    	at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:44)
    	at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
    	at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:123)
    	at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
    	at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
    	at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
    	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:728)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:668)
    	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1205)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1175)
    	... 11 more
    Followed by exceptions in creating sessionFactory, hibernateTemplate, testDao and testService bean.

    Here is my spring config:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:lang="http://www.springframework.org/schema/lang"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.1.xsd
    		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
    
    	<context:spring-configured/>
    
        <context:annotation-config/>
    
        <context:load-time-weaver/>
    
    <bean id="testDao" class="com.amin.atconfigurable.dao.TestDaoImpl" >
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate" />
    		</property>
    	</bean>
    
    	<bean id="testService" class="com.amin.atconfigurable.service.TestServiceImpl" >
    		<property name="testDao">
    			<ref bean="testDao" />
    		</property>
    	</bean>
    
    	<bean id="testBean" class="com.amin.atconfigurable.test.bean.TestBean" scope="prototype">
    		<property name="testService">
    			<ref bean="testService"/>
    		</property>
    
    	</bean>
    I have omitted the hibernate template definition as this is standard stuff but if some needs see it i can post it later.

    Additional info:
    1) I am using LTW using spring-agent in the VM argument

    As I mentioned earlier the service on its own works fine it's when i try to use hibernate it falls down.

    Any help would be much appreciated.

    Thanks

  • #2
    Update:

    After looking further on the forum i found a solution that removes the error described above. The problem now is that my dao is not being called when i create a new instance of my bean. If i remove hibernate reference and just put sysout then it works.

    Here is my updated xml file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:lang="http://www.springframework.org/schema/lang"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.1.xsd
    		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
    
    	<context:spring-configured/>
    
        <context:annotation-config/>
    
        <context:load-time-weaver/>
    
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName">
    			<value>${database.driverclass}</value>
    		</property>
    		<property name="url">
    			<value>${database.url}</value>
    		</property>
    		<property name="username">
    			<value>${databse.username}</value>
    		</property>
    		<property name="password">
    			<value>${database.password}</value>
    		</property>
    	</bean>
    
    
    
    		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect">
    		<property name="dataSource">
    			<ref local="dataSource" />
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>classpath:/com/amin/atconfigurable/test/bean</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="hibernateTemplate"  class="org.springframework.orm.hibernate3.HibernateTemplate" >
    		<property name="sessionFactory">
    			<ref local="sessionFactory"/>
    		</property>
    	</bean>
    
    	<bean id="testDao" class="com.amin.atconfigurable.dao.TestDaoImpl"  depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect">
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate" />
    		</property>
    	</bean>
    
    	<bean id="testService" class="com.amin.atconfigurable.service.TestServiceImpl"  depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect">
    		<lookup-method name="printMessage" bean="testDao"/>
    	</bean>
    
    	<bean id="testBean" class="com.amin.atconfigurable.test.bean.TestBean" scope="prototype" lazy-init="true">
    		<property name="testService">
    			<ref bean="testService"/>
    		</property>
    
    	</bean>
    
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath:server.properties</value>
            </property>
        </bean>
    
    </beans>
    Thanks

    Comment

    Working...
    X