由于Spring工程涉及到两个DataSource的原因,加入另外一个DataSource后,在DAO层有其对应的实现,但是没有写其对应的Hibernate接口。在Eclipse调试窗口出现以下错误:
Injection of autowired dependencies failed; nestedexception is org.springframework.beans.factory.BeanCreationException: Could notautowire method:
public voidcom.xxx.service.impl.ClientServiceImpl.setXXXDao(com.xxx.dao.XXXDAO);
nested exception is java.lang.IllegalArgumentException: argument type mismatch
百思不得其解,各种单步调试、冥思苦想,整整折腾了一天,后来发现是由于以下原因:
The problem is that by default spring uses JDK proxies (java.lang.reflect.Proxy) which are interface-only proxies. Your concreteclass is then used by the invocation handler, but you cannot cast to it.
If there is nointerface, spring uses another method - CGLIB, which subclasses the targetclass in order to make the proxy.
( http://stackoverflow.com/questions/4957299/spring-3-controller-not-able-to-inject-service-object-which-has-transactional> )解决方法有二:
1、加入DAO层对应的Hibernate接口,然后让实现层实现此接口(当然了,在Service层中引用的时候要用DAO层接口!否则要接口还有啥用!!!)
2、在application-dao.xml(名称不一定完全一样)配置文件中,把 <aop:aspectj-autoproxy/> 改成 <aop:aspectj-autoproxyproxy-target-class = "true"/>。
第1种方法肯定是可行的,已验证通过。
第2种方法spring.io官网论坛还有StackOverflow中都有人提及,但是我未验证通过(不知是否是版本问题,特此说明)。
昨天是11.11日,开心每一天!