Perhaps this will be possible using the MethodInvokingFactoryBean class. With this, you can call the method on the target.
You create an object using Spring, and then call addUserName on it.
I'm not sure you can do this with annotations, but with XML you can. For instance:
package some.pack; public class Target { private String userName; public Target() { ... } public void addUserName(String userName) { this.userName = userName; } ... }
You can set the userName property in the Target instance with something like this:
<bean id="target" class="some.pack.Target" /> <bean id="caller" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="target" /> <property name="targetMethod" value="addUserName" /> <property name="arguments"> <list> <value>John_Doe</value> </list> </property> </bean>
The disadvantage of this is that if you need to call several methods, you will need to add a βcallingβ bean for each method. This will increase the size of the XML you must write.
Alternatively, you can use the adapter template to wrap your object in something that conforms to the getXXX / setXXX , and execute DI instead of the standard path on the wrapper.
user159088
source share