有没有办法通过注解来使用查找方法注入(inject)?
给定以下类:
@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
。感谢 Phil Webb .
public class MySingleton {
@Autowired
private Provider<MyPrototype> myPrototype;
public void operation() {
MyPrototype instance = myPrototype.get();
// do something with the instance
}
}
关于spring - 如何使用注解进行 Spring Lookup 方法注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891997/