如何使用注释进行Spring Lookup方法注入? [英] How to do Spring Lookup Method Injection with Annotations?
本文介绍了如何使用注释进行Spring Lookup方法注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下类:
@Service
public abstract class A {
protected abstract createB();
}
为了让它工作,我必须在spring applicationContext.xml如下:
< bean id =bclass =com.xyz.B>
< / bean>
< bean id =aclass =com.xyz.A>
< lookup-method name =createBbean =b/>
< / bean>
即使我使用< context:component-scan base>
我也必须在XML中声明它。
解决方案
可以使用 javax.inject.Provider
。所有感谢您访问 Phil Webb 。
public class MySingleton {
@Autowired $ b $私人提供者< MyPrototype> myPrototype;
public void operation(){
MyPrototype instance = myPrototype.get();
//做一些实例
}
}
Is there any way to use Lookup Method Injection using annotations?
Given the following class:
@Service
public abstract class A {
protected abstract createB();
}
In order to get it to work I have to declare in spring applicationContext.xml the following:
<bean id="b" class="com.xyz.B">
</bean>
<bean id="a" class="com.xyz.A">
<lookup-method name="createB" bean="b"/>
</bean>
Even though I am using <context:component-scan base>
I have to declare it also in the XML. Not a good approach I think.
How to do it with annotations?
解决方案
It is possible to use javax.inject.Provider
. All thanks go to Phil Webb.
public class MySingleton {
@Autowired
private Provider<MyPrototype> myPrototype;
public void operation() {
MyPrototype instance = myPrototype.get();
// do something with the instance
}
}
这篇关于如何使用注释进行Spring Lookup方法注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文