zoukankan      html  css  js  c++  java
  • spring的依赖注入,为什么用接口的实现类而不是父类的继承类?

    @Resource
    private EmployeeService employeeService;

    public void setEmployeeService(EmployeeService employeeService) {
    this.employeeService = employeeService;
    }

    报错:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/login':

    Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:

    Bean named 'employeeService' must be of type [com.xidian.service.imp.EmployeeService], but was actually of type [com.sun.proxy.$Proxy67]

    改成接口方式:

    @Resource
    private EmployeeServiceInter employeeService;

    public void setEmployeeService(EmployeeServiceInter employeeService) {
    this.employeeService = employeeService;
    }

    就可以啦!

    通常是service层需要aop。用接口的话,AOP可以使用java自带的动态代理,但是有点麻烦要写接口。用类就要用第三方包cglib,但是简单,不用写接口

    jdk规定动态代理必须用接口;当然也可以用类,用cglib可以去处理就可以了。

  • 相关阅读:
    数据库存储语句
    数据库练习总结
    数据库练习
    数据库增添
    数据库创建
    cookie 和 session的区别 & 三种传值方式
    内置对象——cookie
    webform跨页面传值
    复合控件
    repeater(控件)数据展示
  • 原文地址:https://www.cnblogs.com/xiangkejin/p/6187116.html
Copyright © 2011-2022 走看看