Is it possible to enter the result of calling a method on a ref bean from Spring?

Is it possible to enter the result of a method call from a ref bean from Spring?

I am trying to reorganize some cut / paste code from two separate projects into a common class. In one of the projects, the code lives in a class, which I will call "MyClient", which is created from Spring. It is introduced by another spring -installed class "MyRegistry", then the MyClient class uses this class to find the endpoint. All I really need is the final String in my refactored class, which can be initialized with Setter. I really cannot have a dependency on MyRegistry on MyClient in the updated code.

So my question is ... there is a way that I can insert the end of the line from Spring that was viewed in the MyRegistry class. So, I have a:

<bean id="registryService" class="foo.MyRegistry"> ...properties set etc... </bean> <bean id="MyClient" class="foo.MyClient"> <property name="registry" ref="registryService"/> </bean> 

But I would like (and I know this is Spring's imaginary syntax)

 <bean id="MyClient" class="foo.MyClient"> <property name="endPoint" value="registryService.getEndPoint('bar')"/> </bean> 

where MyRegistry will have a getEndPoint (Stringng endPointName) method

Hope this makes sense in terms of what I'm trying to achieve. Please let me know if something like this is possible in Spring!

+59
java spring
Mar 26 '10 at 1:52
source share
3 answers

The most enjoyable solution is to use the Spring 3 expression language as described in @ ChssPly76, but if you are using an older version of Spring, it is almost as simple:

 <bean id="MyClient" class="foo.MyClient"> <property name="endPoint"> <bean factory-bean="registryService" factory-method="getEndPoint"> <constructor-arg value="bar"/> </bean> </property> </bean> 
+38
Mar 26 '10 at 8:08
source share

This is possible in Spring 3.0 through the 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> 
+98
Mar 26
source share

Or in Spring 2.x using BeanPostProcessor

Typically, bean post processors are used to validate the properties of a bean or change the bean properties (whatever you want) according to specific criteria.

 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

 <bean class="br.com.somthing.MyClientBeanPostProcessor"/> 
+2
Mar 26 '10 at 4:40
source share



All Articles