注釈でスプリングルックアップメソッドインジェクションを行うにはどうすればよいですか?
-
28-09-2019 - |
質問
注釈を使用してルックアップメソッドインジェクションを使用する方法はありますか?
次のクラスが与えられます:
@Service
public abstract class A {
protected abstract createB();
}
それを動作させるためには、Spring ApplicationContext.xmlで次のように宣言する必要があります。
<bean id="b" class="com.xyz.B">
</bean>
<bean id="a" class="com.xyz.A">
<lookup-method name="createB" bean="b"/>
</bean>
私が使用しているにもかかわらず <context:component-scan base>
XMLでも宣言しなければなりません。良いアプローチではありません。
注釈でそれを行う方法は?
解決
使用することができます javax.inject.Provider
. 。すべてに感謝します フィルウェッブ.
public class MySingleton {
@Autowired
private Provider<MyPrototype> myPrototype;
public void operation() {
MyPrototype instance = myPrototype.get();
// do something with the instance
}
}
他のヒント
でも可能です org.springframework.beans.factory.ObjectFactory
Spring APIに遅れずについていく場合
public class MySingleton {
@Autowired
private ObjectFactory<MyPrototype> myPrototypeFactory;
public void operation() {
MyPrototype instance = myPrototypeFactory.getObject();
// do something with the instance
}
}
詳細を読むことができます ドキュメンテーション.
spring> = 4.1でのみ実装されます チケット.
所属していません StackOverflow