簡體   English   中英

......不是訪問方法

[英]… is no accessor method

我正在學習Spring的繩索。
我嘗試自動裝配一個方法參數,如下所示:

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

使用以下接口和類:

@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";
    }

}

但是,在調用控制器的方法時,我得到一個IllegalArgumentException
Invoked method public abstract void AnInterface.doSomething() is no accessor method!

我究竟做錯了什么?

提前致謝 :)

你不能用那種方式自動裝配組件,試試這個:

@Autowire AnInterface ai;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM