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.
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.
You could try with @Autowire
.
In the classes where you need the MyObject bean, you could use something like this:
public class MyClass {
@Autowire
MyObject myObject;
public void myMethodofMyClass(){
myObject.oneMethodOfMyObject();
}
}
You can find more info in the spring reference manual (page 54).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With