【问题标题】:Error autowiring an interface class in Spring在 Spring 中自动装配接口类时出错
【发布时间】:2015-10-14 20:11:33
【问题描述】:

我的代码有这个设置:

public class myResource() {
   @Autowired
   ImyHandler myHandler;

   ....
}

下一层包含我的处理程序类。我有两套类,一套是接口类,一套是普通类,比如:

public interface myHandler() {
      public myObject modifyFunction();
}

然后我的常规课看起来像:

public class myHandler() {
     ....
}

但是,当我构建我的项目时,我收到了这个错误:

Error creating bean with name 'myResource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: ImyHandler

在我的context-web.xml 文件中,我声明:

<bean id="myHandler" class="myHandler" />

我不确定为什么会收到此错误,因为我明确指出接口类在我的上下文中。有谁明白为什么会这样?

【问题讨论】:

  • 实现接口的类在哪里?你忘了让myHandler类实现接口ImyHandler吗? public class myHandler implements ImyHandler
  • public class myHandler() {} 无法编译。
  • 您在此处发布的每一行代码都是错误的...无论是 java、spring 还是其他...您尝试编译任何东西?
  • @Pras 是的,它可以编译,这都是伪代码,我知道它不会编译。我只需要在“myHandler”方法声明中添加“implements ImyHandler”。
  • 你甚至没有一个叫 IMyHandler 的接口

标签: java spring


【解决方案1】:

您还需要在要创建的bean中指定类的包。

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 2015-02-22
    • 2011-09-24
    • 1970-01-01
    • 2015-05-27
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多