2016-12-05 16 views
1

現在、私はSpringのロープを習っています。
私はautowireそうのようなメソッドのパラメータに試してみました:次のインタフェースとクラスで...アクセサメソッドがありません

@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!

私は間違っていますか?事前に

感謝:)

+0

あなたはこのコードを実行しましたが?コンパイル時エラーが発生すると思います。 – SachinSarawgi

+0

ええ、それは私のために働く。 しかし、コードをコピーしているうちにエラーが発生した可能性があります。 エラーはどうなりますか? – portux

答えて

2

あなたは、これを試してみてください方法コンポーネントautowireすることはできません。

@Autowire AnInterface ai; 

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

それはうまくいきました。ありがとうございました! それは少し外れているかもしれませんが、それはなぜですか? – portux

+0

@rbgm、アプリケーションが起動すると春のBeanが初期化され、その時点で既に実行中のアプリケーションでメソッドが呼び出されます。 – Jaiwo99

関連する問題