关于Autowiring Class Issue (argument type mismatch)的问题的解决方法

由于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日,开心每一天!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值