|
From: Rod J. <rod...@in...> - 2004-06-28 11:56:44
|
To round out our Method Injection support, I've just added the ability for
the container to override a method to replace it with an arbitrary piece of
code, in an implementation of the MethodReplacer interface.
The XML syntax looks as follows:
<replaced-method name="doSomething"
replacer="beanNameOfReplacer">
</replaced-method>
This is a less commonly useful feature than lookup Method Injection, but it
has certain uses. For example, a protected setRollbackOnly method could be
overridden by the container to use the TransactionContext, avoiding Spring
dependencies in testing. I would envisage most of the usages, like this one,
involving a fairly generic, reusable MethodReplacer.
An example of what could be done: a getDataSource() method could be
overridden to return a transactional DataSource if there's an active
transaction, otherwise a read-only DataSource. Of course this kind of thing
can often be done with a custom TargetSource. However, there is a difference
here: Method Injection actually changes the class, so the class can invoke
methods on itself and get the new behaviour without going through a proxy.
I will try to put together examples and usage guidelines before 1.1 final is
released. If you're interested in the meantime, take a look at
XmlBeanFactoryTestSuite. I see Method Injection as very useful in a number
of corner cases, but not something to use without good reason.
Rgds
Rod
|