Hi all,
I've read this article which talks about a way to avoid duplicating DAO codes.
Now i've troubles when implementing it. I've search this forum and found several threads describing the same problem :
Especially the second threads details exactly the same problem I have, even the stack strace.
To sum up:
We have the following context configuration:
Code:
<bean id="finderIntroductionAdvisor" class="dao.impl.hibernate.FinderIntroductionAdvisor"/>
<bean id="abstractDao" class="org.springframework.aop.framework.ProxyFactoryBean" abstract="true">
<property name="interceptorNames">
<list>
<value>finderIntroductionAdvisor</value>
</list>
</property>
</bean>
<bean id="abstractDaoTarget" class="dao.impl.hibernate.GenericDao" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="TicketDao" parent="abstractDao">
<property name="proxyInterfaces">
<value>dao.ITicketDao</value>
</property>
<property name="target">
<bean parent="abstractDaoTarget">
<constructor-arg>
<value>model.security.Ticket</value>
</constructor-arg>
</bean>
</property>
</bean>
and the following code :
Code:
public class FinderIntroductionAdvisor extends DefaultIntroductionAdvisor {
public FinderIntroductionAdvisor() {
super(new FinderIntroductionInterceptor());
}
}
Code:
public class FinderIntroductionInterceptor implements IntroductionInterceptor {
private static Log logger = LogFactory.getLog(FinderIntroductionInterceptor.class);
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
logger.debug("Intercepting " + methodInvocation.getMethod() + "on " + methodInvocation.getThis());
FinderExecutor genericDao = (FinderExecutor) methodInvocation.getThis();
String methodName = methodInvocation.getMethod().getName();
if (methodName.startsWith("find")) {
Object[] arguments = methodInvocation.getArguments();
return genericDao.executeFinder(methodInvocation.getMethod(), arguments);
}
else {
return methodInvocation.proceed();
}
}
public boolean implementsInterface(Class intf) {
logger.debug("Implements interface " + intf.getCanonicalName() + "?");
return intf.isInterface() && FinderExecutor.class.isAssignableFrom(intf);
}
Stack trace shows something like that:
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RepositoryService' defined in ServletContext resource [/WEB-INF/business.xml]: Cannot resolve reference to bean 'NodeDao' while setting bean property 'nodeDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'NodeDao' defined in ServletContext resource [/WEB-INF/domain.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/domain.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.apache.commons.dbcp.BasicDataSource
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'NodeDao' defined in ServletContext resource [/WEB-INF/domain.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/domain.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.apache.commons.dbcp.BasicDataSource
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/domain.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.apache.commons.dbcp.BasicDataSource
Caused by:
java.lang.ClassCastException: org.apache.commons.dbcp.BasicDataSource
at org.escapek.core.dao.impl.hibernate.FinderIntroductionInterceptor.invoke(FinderIntroductionInterceptor.java:14)
It seems that ProxyFactoryBean invoke method is called for any class , not only for classes specified in the ProxyFactoryBean target property.
Unfortunatelly, i couldn't find real answer on both threads. May be one on their authors have found the solution since then.
Thanks for your help,
Nico.