We have a lot of DAO in project and their own implementations. For example, I have a class called CorDAO and CorDAOHibernate (CorDAO it's an interface and CorDAOHibernate implements CorDAO). For each interface I need do this:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.myproject.*.to"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<bean id="genericDAO" class="com.myproject.dao.hibernate.GenericDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="CorDAO" class="com.myproject.dao.hibernate.CorDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="ExampleDAO" class="com.myproject.dao.hibernate.ExampleDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
This it's why I wanna use ComponentScan in com.myproject.dao.hibernate.* package. Note that CorDAO it's a @Repository and CorDAOHibernate doesn't have any annotation. Every DAO class is a child of GenericDAO.
How can I use ComponentScan? I'm using Spring 3 so I tried this:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.myproject.*.to"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<context:component-scan base-package="com.myproject.dao.hibernate" />
And I put a @Component annotation in CorDAOHibernate and GenericDAOHibernate but I got an IllegalArgumentException:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'corDAOHibernate' defined in URL [jar:file:/home/danielamorais/Documents/apache-tomcat-8.0.47/webapps/myproject/WEB-INF/lib/myproject-3.1.0.jar!/com/myproject/dao/hibernate/corDAOHibernate.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
I think this happens because sessionFactory it's autowired in GenericDAOHibernate but is null in CorDAOHibernate. How can I fix this with component scan?
Classes CorDAO
@Repository
public interface CorDAO extends GenericDAO {
//Methods
}
CorDAOHibernate
@Component
public class CorDAOHibernate extends GenericDAOHibernate implements CorDAO {
//Methods
}
GenericDAO
public interface GenericDAO extends DAO {
//Methods. This class doesn't contains @Repository annotation
}
GenericDAOHibernate
@Component
public class GenericDAOHibernate extends HibernateDaoSupport implements GenericDAO, PropertySelector {
@Autowired
private SessionFactory sessionFactory;
protected Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
//Methods
}
DAO
public interface DAO {
//There's no method here.
}
Also, adding @Component("genericDAO") I got (probably because an inheritance problem):
NoUniqueBeanDefinitionException: No qualifying bean of type [com.myproject.dao.GenericDAO] is defined: expected single matching bean but found 88