Java 是否可以从 Spring 注入在 ref bean 上调用方法的结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2520722/
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
Is it possible from Spring to inject the result of calling a method on a ref bean?
提问by Alex Worden
Is it possible from Spring to inject the result of calling a method on a ref bean?
是否可以从 Spring 注入在 ref bean 上调用方法的结果?
I'm trying to refactor some cut/pasted code from two separate projects into a common class. In one of the projects, the code lives in a class I'll call "MyClient" that is being instantiated from Spring. It is injected with another spring-instantiated class "MyRegistry", then the MyClient class uses that class to look up an endpoint. All I really need is the endpoint String in my refactored class, which can be initialized via a Setter. I really cannot have a dependency on MyRegistry from MyClient in the refactored code.
我正在尝试将两个独立项目中的一些剪切/粘贴代码重构为一个公共类。在其中一个项目中,代码位于一个我称为“MyClient”的类中,该类正在从 Spring 实例化。它注入了另一个 spring 实例化类“MyRegistry”,然后 MyClient 类使用该类来查找端点。我真正需要的是重构类中的端点字符串,它可以通过 Setter 进行初始化。在重构的代码中,我真的无法依赖 MyClient 中的 MyRegistry。
So, my question is this... is there a way I can inject the endpoint String from spring that was looked up in the MyRegistry class. So, I currently have:
所以,我的问题是......有没有办法可以从在 MyRegistry 类中查找的 spring 注入端点字符串。所以,我目前有:
<bean id="registryService" class="foo.MyRegistry">
...properties set etc...
</bean>
<bean id="MyClient" class="foo.MyClient">
<property name="registry" ref="registryService"/>
</bean>
But I'd like to have (and I know this is imaginary Spring syntax)
但我想要(而且我知道这是虚构的 Spring 语法)
<bean id="MyClient" class="foo.MyClient">
<property name="endPoint" value="registryService.getEndPoint('bar')"/>
</bean>
where MyRegistry will have a method getEndPoint(Stirng endPointName)
其中 MyRegistry 将有一个方法 getEndPoint(Stirng endPointName)
Hope that makes sense from a the standpoint of what I'm trying to achieve. Please let me know if something like this is possible in Spring!
希望从我正在努力实现的角度来看,这是有道理的。请让我知道这样的事情在春天是否可能发生!
采纳答案by skaffman
The nicest solution is to use Spring 3's expression language as described by @ChssPly76, but if you're using an older version of Spring, it's almost as easy:
最好的解决方案是使用 @ChssPly76 所描述的 Spring 3 表达式语言,但如果您使用的是旧版本的 Spring,它几乎同样简单:
<bean id="MyClient" class="foo.MyClient">
<property name="endPoint">
<bean factory-bean="registryService" factory-method="getEndPoint">
<constructor-arg value="bar"/>
</bean>
</property>
</bean>
回答by ChssPly76
It's possible in Spring 3.0 via Spring Expression Language:
在 Spring 3.0 中可以通过Spring Expression Language 实现:
<bean id="registryService" class="foo.MyRegistry">
...properties set etc...
</bean>
<bean id="MyClient" class="foo.MyClient">
<property name="endPoint" value="#{registryService.getEndPoint('bar')}"/>
</bean>
回答by Arthur Ronald
Or in Spring 2.x, by using a BeanPostProcessor
或者在 Spring 2.x 中,通过使用 BeanPostProcessor
Typically, bean post processors are used for checking the validity of bean properties or altering bean properties(what you want to) according to particular criteria.
通常,bean 后处理器用于根据特定标准检查 bean 属性的有效性或更改 bean 属性(您想要什么)。
public class MyClientBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if((bean instanceof MyClient)) && (beanName.equals("MyClient"))) {
Myregistry registryService = (Myregistry) applicationContext.getBean("registryService");
((MyClient) bean).setEndPoint(registryService.getEndPoint("bar"));
}
return bean;
}
}
And register your BeanPostProcessor
并注册您的 BeanPostProcessor
<bean class="br.com.somthing.MyClientBeanPostProcessor"/>