I was fairly confused for awhile when using MethodInvokingFactoryBean, and
found out that the fancy conversions you use when defining properties on
beans don't happen to the arguments passed into the
MethodInvokingFactoryBean.
I thought this was rather un-Spring like, personally, but assumed it was an
oversight, or hadn't been needed, or whatever.
I whipped up the following subclass that sorts things out nicely. It's a bit
naive, and it doesn't find any registered PropertyEditors defined in the
context or anything, but it works for the bare bones stuff.
As a side note, I found that there really isn't any API support for finding
registered custom property editors in the context - the only thing that was
available to use was AbstractBeanFactory.getCustomEditors(). I was thinking
that it would be handy for _some_ interface to expose this - possibly
ConfigurableBeanFactory, where registerCustomEditor is defined.
Anyway, here's the class. I just slapped it into my project, I've never
set up a build of spring on my local box (I haven't had to - it works great :)
so I haven't tried to make it official, written tests, or anything like that.
Maybe this is something that could get included, or maybe the functionality
folded in MethodInvokingFactoryBean?
H
----
public class TypeConvertingMethodInvokingFactoryBean extends
MethodInvokingFactoryBean
{
public void prepare() throws ClassNotFoundException, NoSuchMethodException
{
super.prepare();
BeanWrapperImpl wrapper = new BeanWrapperImpl();
Object[] args = getArguments();
Method method = getPreparedMethod();
Class[] requiredTypes = method.getParameterTypes();
for (int i=0; i < args.length; i++)
args[i] = wrapper.doTypeConversionIfNecessary(args[i],
requiredTypes[i]);
setArguments(args);
}
}
|