I have created two transaction managers(one for READ requests and another for READ-WRITE requests) and two session factories for the same.
Important snippets are as follows:
<tx:annotation-driven />
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="txManagerRead"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
<qualifier value="READ"/>
</bean>
<bean id="txManagerWrite"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="Session1" />
<qualifier value="WRITE"/>
</bean>
<bean id="AbstractSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
abstract="true">
<property name="annotatedClasses">
<list>
.....
....
</list>
</property>
<property name="exposeTransactionAwareSessionFactory">
<value>true</value>
</property>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
parent="AbstractSessionFactory">
<property name="hibernateProperties">
<props>
.........
.........
</props>
</property>
</bean>
<bean id="SessionFactory1"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
parent="AbstractSessionFactory">
<property name="hibernateProperties">
<props>
.........
.........
</props>
</property>
</bean>
The moment I am trying to create a bean for DAO, like:
<bean id="configurationDAO" class="com.amazon.im.dao.hibernate.ConfigurationDAOImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
I am getting BeanCreationException with following error message:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘configurationDAO’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.hibernate.SessionFactory com.amazon.im.dao.hibernate.GenericDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [SessionFactory, SessionFactory1]
I cannot use LocalSessionFactoryBean because I want to use “annotatedClasses” property.
Any pointers on why is it happening?
Is there anything missing from my side?
It look like the
sessionFactoryproperty of yourGenericDAOImplclass (which I assume is the superclass ofConfigurationDAOImpl?) is annotated with@Autowired. This won’t work because it has two session factories to choose from. Also, you’re manually injecting the correct session factory using<property>, so the@Autowiredisn’t necessary.