Spring动态代理后,自动注入被代理的原对象为什么会报错?
这是service接口
这是service接口的实现类
这是controller方法
在spring中对service的实现类(AccountServiceImpl)进行声明式事务控制,报错,报错核心信息是
Bean named 'accountService' is expected to be of type 'com.service.impl.AccountServiceImpl' but was actually of type 'com.sun.proxy.$Proxy24'
找到原因是spring的事务控制是对AccountServiceImpl进行基于接口的动态代理,将controller中的自动注入类型改为
即可。那么我就很奇怪,动态代理的原对象肯定也是存在的,为什么自动注入原对象类型会报错呢?
而且报错的信息除了注入失败以外,最主要的还是spring容器中id为accountService的bean类型本应该为AccountServiceImpl类型。spring是怎么知道bean对象原本类型的?
public interface AccountService {
public List<Account> findAll();
}
这是service接口的实现类
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
AccountDao accountDao;
@Override
public List<Account> findAll() {
System.out.println("业务层,查询所有账户信息。。。");
return accountDao.findAll();
}
}
这是controller方法
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountServiceImpl accountService1;
@RequestMapping("/findAll")
public String findAll(){
System.out.println("表现层--AccountController.findAll()");
List<Account> list = accountService1.findAll();
for(Account a:list){
System.out.println(a);
}
return "success";
}
}
在spring中对service的实现类(AccountServiceImpl)进行声明式事务控制,报错,报错核心信息是
Bean named 'accountService' is expected to be of type 'com.service.impl.AccountServiceImpl' but was actually of type 'com.sun.proxy.$Proxy24'
找到原因是spring的事务控制是对AccountServiceImpl进行基于接口的动态代理,将controller中的自动注入类型改为
@Autowired
private AccountService accountService1;
即可。那么我就很奇怪,动态代理的原对象肯定也是存在的,为什么自动注入原对象类型会报错呢?
而且报错的信息除了注入失败以外,最主要的还是spring容器中id为accountService的bean类型本应该为AccountServiceImpl类型。spring是怎么知道bean对象原本类型的?
...全文
相关推荐
spring多个动态代理导致循环依赖报错问题解决
NULL 博文链接:https://zheng19851.iteye.com/blog/2070030
详解Spring @Autowired 注入小技巧
主要介绍了详解Spring @Autowired 注入小技巧,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
因Spring AOP导致@Autowired依赖注入失败的解决方法
主要给大家介绍了因Spring AOP导致@Autowired依赖注入失败的解决方法,文中通过示例代码给大家介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
ssm例子一直报错userDao无法注入
哪个大神帮忙看看,谢谢啦
spring+springmvc整合mabytis时mapper注入失败问题解决方法
主要介绍了spring+springmvc整合mabytis时mapper注入失败问题解决方法 ,需要的朋友可以参考下


