java - ... 不是访问器方法

标签 java spring

我目前正在学习 Spring 的诀窍。
我尝试像这样 Autowiring 一个方法参数:

@RequestMapping("/hello")
public String something(@Autowire AnInterface ai) {
    ai.doSomething();
    return "/";
}

具有以下接口(interface)和类:

@Component
public interface AnInterface {
    void doSomething();
}

@Component
public class Implementation implements AnInterface {
    @Autowired private AnotherInterface ai;

    public Implementation() { ; }

    public Implementation(AnotherInterface ai) {
        this.ai = ai;
    }

    public void setAi(AnotherInterface ai) {
        this.ai = ai;
    }

   @Override
   public void doSomething() {
       System.out.println(ai.hello());

   }
}

@Component
public interface AnotherInterface {
    String hello();
}

@Component
public class AnotherImplementation implements AnotherInterface {

    @Override
    public String hello() {
        return "hello";
    }

}

但是,当调用 Controller 的方法时,我得到一个IllegalArgumentException:
调用的方法 public abstract void AnInterface.doSomething() 不是访问器方法!

我做错了什么?

提前致谢:)

最佳答案

你不能那样 Autowiring 组件,试试这个:

@Autowire AnInterface ai;

@RequestMapping("/hello")
public String something() {
    ai.doSomething();
    return "/";
}

关于java - ... 不是访问器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40972012/

相关文章:

java - 在软件中实现超时

java - 如何在 unix 上运行 .jar 文件?

java - 浏览器显示陈旧的 JSP 页面,用户必须手动刷新

java - 使用Java在Android中添加20个textView

java - 了解AppKey安全性(无 secret )

java - Spring MVC 和 Apache POI 创建 xls 文档。无法保存模型中新创建的文件

java - 如何检索 JSP 页面中 POJO 字段的注释(JPA 和 Hibernate)

java - 将字符串转换为数字并返回自然语言?

java - 在 spring 3 mvc 应用程序中使用 hibernate 的步骤

spring - 使用 Spring 管理 Hibernate session