This can be done somewhat simply using xml, as described in sections 3.3.2.2 and 3.3.2.3 of the Spring documentation.
In conclusion, section 3.3.2.2 provides a method for invoking the static method on a class. This can be done like this:
<bean id="myBean" class="com.foo.MyClass" factory-method="myStaticMethod"/>
This will create a bean in the ApplicationContext named myBean , which is populated with the returned object from invokation MyClass.myStaticMethod() .
But this is only halfway, since we only have the result of the static method (the first call in your case).
Section 3.3.2.3 details how to call the non-static bean method that already exists in the ApplicationContext . This can be done like this:
<bean id="myOtherBean" factory-bean="myBean" factory-method="myNonStaticMethod"/>
This will create a bean in the ApplicationContext named myOtherBean , which is populated with the returned object from invokation myBean.myNonStaticMethod() , where myBean is the bean extracted from the ApplicationContext .
When you come together, you can achieve what you need.
<bean id="localhostInetAddress" class="java.net.InetAddress" factory-method="getLocalHost"/> <bean id="hostname" factory-bean="localhostInetAddress" factory-method="getHostName"/>
Of course, an easier way to do this is to configure Java .
@Configuration public class InetConfig { @Bean public String hostname() { return InetAddress.getLocalHost().getHostName(); } }
nicholas.hauschild
source share