Java 如何使用 Spring 属性配置 Spring 查找方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20261769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Spring lookup-method with Spring property
提问by blue-sky
I'm trying to inject a property everytime a bean (myBean) is called using a lookup method and Spring dependency injection :
每次使用查找方法和 Spring 依赖注入调用 bean (myBean) 时,我都尝试注入一个属性:
<bean id="myBean" class="com.myclass"
<property name="default" ref="myDefault" >
<lookup-method name="getUri" bean="defaultUri" />
</property>
</bean>
<bean id="defaultUri" scope="prototype" class="DefaultUri" >
</bean>
class myclass {
public String getUri(){
return "test"
}
}
Above XML returns this error on startup :
以上 XML 在启动时返回此错误:
"XML document from PortletContext resource is invalied"
“来自 PortletContext 资源的 XML 文档无效”
The error seems to be because <lookup-method name="getUri" bean="defaultUri" />
is configured incorrectly.
错误似乎是因为<lookup-method name="getUri" bean="defaultUri" />
配置不正确。
How can I configure a Spring lookup method within a String 'property' as I'm trying to implement in above XML ?
当我试图在上面的 XML 中实现时,如何在字符串“属性”中配置 Spring 查找方法?
采纳答案by Debojit Saikia
Lookup method injection is the ability of the container to override methods on container managed beans, to return the lookup result for another named bean in the container.
查找方法注入是容器覆盖容器管理 bean 上的方法的能力,以返回容器中另一个命名 bean 的查找结果。
Now, suppose you want to get a new instance of DefaultUri
(which is a prototype bean) every time you call a method (let it be createDefaultUri
) in myclass
(which is a singleton bean). Then you can define MyClass
as this:
现在,假设您想在DefaultUri
每次调用方法(让它成为createDefaultUri
) in myclass
(它是一个单例 bean)时获得一个新实例(它是一个原型bean)。然后你可以这样定义MyClass
:
class abstract Myclass {
public String getUri(){
// create a new instance of DefaultUri
DefaultUri defaultUri = createDefaultUri();
return "test"
}
protected abstract DefaultUri createDefaultUri();
}
The Spring Framework will generate a dynamic subclass of Myclass
that will override the createDefaultUri
method to provide a new instance of DefaultUri
every time it is requested for.
Spring Framework 将生成一个动态子类,Myclass
该子类将覆盖该createDefaultUri
方法以在DefaultUri
每次请求时提供一个新实例。
You can now define the name of lookup-method
name in the Myclass
bean definition as this:
您现在可以lookup-method
在Myclass
bean 定义中定义name 的名称,如下所示:
<bean id="defaultUri" scope="prototype" class="DefaultUri">
</bean>
<bean id="myBean" class="com.myclass"
<lookup-method name="createDefaultUri" bean="defaultUri" />
</bean>
回答by techiesantosh
Suppose singleton bean A needs to use non-singleton (prototype) bean B, perhaps on each method invocation on A(getBeanB()), we expect to get new instance of bean B for every request. But The container only creates the singleton bean A once, and thus only gets one opportunity to set the properties. The container cannot provide bean A with a new instance of bean B every time one is needed. To get new new instance of bean B for every request we need to use lookup-method injection
假设单例 bean A 需要使用非单例(原型)bean B,也许在 A(getBeanB()) 上的每个方法调用中,我们希望为每个请求获得 bean B 的新实例。但是容器只创建了一次单例bean A,因此只有一次设置属性的机会。容器无法在每次需要时为 bean A 提供 bean B 的新实例。要为每个请求获取 bean B 的新实例,我们需要使用查找方法注入
Please refer to http://www.javapointer.com/spring/spring-core/spring-lookup-method-injection/
请参考http://www.javapointer.com/spring/spring-core/spring-lookup-method-injection/