Create a new object and call a method spring configuration

java, spring

Solution

Have you considered using an anonymous bean in your `MethodInvokingFactoryBean`?

<bean id="data" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject"><bean class="MyObject" /></property>
    <property name="targetMethod"><value>getData</value></property>
</bean>

This is basically equivalent to what you have, except for the fact that there is no intermediate bean definition. I am not sure whether or not the `MyObject` instance will be in your `ApplicationContext`, though, so if you need it there, you may want to look into that.

Problem

Is there a short cut way to implement the following functionality in Spring xml configuration file. ``` new MyObject().getData() ``` instead of ``` Object obj = new MyObject(); obj.getData(); ``` I know how to do it in 2nd case. ``` <bean id="obj" class="MyObject" /> <bean id="data" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"><ref local="obj" /></property> <property name="targetMethod"><value>getData</value></property> </bean> ``` I am sure there must be a way to do this in a single definition. Please suggest.

Original source