我的Action是这样的package fi.finance.web.action;
public class FinanceAction extends ActionSupport {
//注意这里不是接口而是实现类
private CustomerService customerService;

public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
我的applicationContext.xml文件中关于这个action配置是这样的<bean id="customerService"  class="fi.customer.service.CustomerService" /><bean id="financeAction"  class="fi.finance.web.action.FinanceAction">
<property name="customerService" ref="customerService" />
</bean>但是我在Myeclipse中启动工程是就报错了,错误信息是:Cannot convert value of type [$Proxy15 implementing fi.customer.service.iface.ICustomerService,fi.common.service.iface.IBaseService,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [fi.customer.service.CustomerService] for property 'customerService': no matching editors or conversion strategy found我把Action中的private CustomerService customerService;改为private ICustomerService customerService;就好了,为什么注入service的实现类就报错了呢?我原来也只知道要定义接口,但是为什么要定义成接口呢?高手给说下原理,或者给个相关的链接也行还有一点要说的,我把工程弄在Eclipse下,用tomcat启用又不报错,真是郁闷,什么原因啊?

解决方案 »

  1.   

    注入实现类应该是可以实现的,我最近做了项目在myEclipse中实现过,至于你这为什么不可以,我还真不知道,期待老鸟…………学习
      

  2.   

    依赖注入的原理就是面向接口编程,把类的实例化交给外部容器(如spring)处理。这样的好处是解耦。所以要用接口而不能是实现类,你这样用实现类就违背的依赖注入的初衷了。
      

  3.   

    MVC  这个能混淆吗? 各自的功能不能颠倒
      

  4.   

    把你ICustomerService 的定义贴出来看一下
      

  5.   

    在Action中定义接口引用,很好啊,解耦,扩充性强啊!为什么非要用真实实现类来注入啊?没必要啊!
    依赖注入的原理是依托Web容器的,工程启动的时候,通过监听器,然后加载Spring的bean配置文件,然后各种bean的配置的包装方法不同,一般默认的时容器启动时,各个bean被实例化一个,放在容器中,然后有需要调用的时候,都是使用接口引用的setter方法,就是这样。
      

  6.   

    package fi.customer.service.iface;import java.util.List;
    import java.util.Map;import fi.common.service.iface.IBaseService;
    import fi.common.web.Pager;
    import fi.customer.bean.IDSCustomer;public interface ICustomerService extends IBaseService<IDSCustomer> {

    public long saveCustomer(IDSCustomer customer);

    public String getCustomerNumber(long cusotomerId); public void updateCustomer(IDSCustomer customer);

    public IDSCustomer findCustomer(Long customerId);}
      

  7.   

    你说的这些我都知道,我也知道用接口可以解藕,但是那也不应该是强制规定吧,如果我不是接口的话就报错吧,spring为什么一定要注入一个接口呢?不定义接口为什么就报错了呢?这是我想问的?
      

  8.   


    不定义接口为什么就报错了呢?
    这是楼主自己写的有问题吧。 Spring提倡面向接口编程,但是绝对不是强制面向接口编程。
      

  9.   


    package fi.finance.web.action;
    public class FinanceAction extends ActionSupport {
        //注意这里不是接口而是实现类
        private CustomerService customerService;
        
        public CustomerService getCustomerService() {
            return customerService;
        }
        public void setCustomerService(CustomerService customerService) {
            this.customerService = customerService;
        }
    }<bean id="customerService"  class="fi.customer.service.CustomerService" />
    在Java代码里面没有看到import fi.customer.service.CustomerService;语句,不知道是楼主没有帖上来,还是说CustomerService与FinanceAction在同一个包目录下面呢?
    但是我从上面配置与FinanceAction的包目录,明显2个类不同一个包下面。