我目前正在学习 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/