JIT
jit · 2004-12-23
<< Enum example of Java
|
首页
|
2004-12-23
Spring method replacement
Tag:
Spring
版权声明
:转载时请以超链接形式标明文章原始出处和作者信息及
本声明
http://jit.blogbus.com/logs/552696.html
The latest versions of
Spring
introduce two new types of method injection: lookup methods and method replacement.
I have some thoughts concerning method replacement, so that is where I'll place the focus of this blog entry. (I may blog about lookup methods another time, but for today it's method replacement.)
Let's say you have the following class:
package com.habuma.wiring;
public class FriendlyImpl implements Friendly {
public void sayHello(String name) {
System.out.println("Hello "+ name);
}
}
The
sayHello()
method is quite straightforward. But suppose that instead of an English version, you'd like
sayHello()
to have a Spanish flair. The obvious way to fix this is to make the change and recompile. But what if you don't own the source code for
FriendlyImpl
? In that case, maybe you could subclass
FriendlyImpl
and override
sayHello()
. But that's the boring way of doing it (that's
so
1995). Let's look at a Spring way of overriding
sayHello()
.
First, you would create a replacer class that will implement the new
sayHello()
method. To do that you must implement Spring's
MethodReplacer
interface. For our Spanish
sayHello()
the replacer might look like this:
package com.habuma.wiring;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class FriendlyReplacer implements MethodReplacer {
public Object reimplement(Object o, Method m, Object[] a)
throws Throwable {
System.out.println("Hola "+a[0]);
return null;
}
}
The
reimplement()
method is where the new functionality will be defined. If you've worked with dynamic proxies, then
reimplement()
should be somewhat familiar as it is quite similar to
InvocationHandler
's
invoke()
method.
Now all that's left to do is to tell Spring to use
FriendlyReplacer
to override
FriendlyImpl
's
sayHello()
method. You do this with the
<replaced-method>
element in the Spring configuration file:
<beans>
<bean id="friendly"
class="com.habuma.wiring.FriendlyImpl">
<replaced-method name="sayHello" replacer="friendlyReplacer">
<arg-type>String</arg-type>
</replaced-method>
</bean>
<bean id="friendlyReplacer"
class="com.habuma.wiring.FriendlyReplacer"/>
</beans>
When Spring loads its context, it will subclass
FriendlyImpl
, overriding its
sayHello()
method with a new implementation as defined in
FriendlyReplacer
. (In case you're wondering...if
sayHello()
is
final
, then it will not be overridden. If
FriendlyImpl
is
final
, Spring will throw an exception.)
This is pretty cool stuff. But as cool as it may be, is it really practical? Why would I do this in Spring instead of simply creating a subclass of
FriendlyImpl
that overrides
sayHello()
to be in Spanish? After all, subclassing in Java is much more straightforward than creating a replacer class.
Let me try to answer my own question and you tell me if I'm getting it wrong. The
Friendly
example above is far too simple to highlight the power of
<replaced-method>
. We need a slightly more interesting example.
Suppose that, instead of
FriendlyImpl
, we have a class that represents a record and playback device:
public abstract class AbstractRecorder implements Recorder {
public abstract void record();
public abstract void playback();
}
The actual implementations of
record()
and
playback()
are left abstract (
<replaced-method>
works just fine with abstract methods). This leaves it up to me to create implementations of these methods to define the mechanisms by which recording and playback are performed.
For example, I could implement
record()
as recording to tape, CD, or MP3. At the same time, I could implement
playback()
to play to a speaker, over the radio airwaves, or maybe across a phone line.
If I were to create subclasses to cover all of the possible combinations of
record()
and
playback()
implementations mentioned above, I'd end up with at least 9 subclasses of
AbstractRecorder
(3 recording implementations times 3 playback implementations). If I were to add another recording implementation I'd end up creating 3 more subclasses; one for each of the playback implementations.
But if I were to use Spring's
<replaced-method>
I'd only need to create one replacer class for each recording implementation and one replacer class for each playback implementation. Then I'd piece together the chosen record-playback combination in Spring's configuration file when I declare the "recorder" bean:
<beans>
<bean id="recorder" class="com.habuma.wiring.AbstractRecorder">
<replaced-method name="record" replacer="recordDevice"/>
<replaced-method name="playback" replacer="playbackDevice"/>
</bean>
<bean id="recordDevice"
class="com.habuma.wiring.TapeRecorder"/>
<bean id="playbackDevice"
class="com.habuma.wiring.SpeakerPlayback"/>
</beans>
As wired here, I've declared the "recorder" bean to be a simple tape recorder. It records to tape and plays back to a speaker. If I decide later that I need the playback to be over the radio instead a speaker, I just switch the "playbackDevice" bean's class to be "com.habuma.wiring.RadioPlayback". Likewise, I can swap out the "recordDevice" bean's class with "com.habuma.wiring.Mp3Recorder" to record to MP3.
Again, I'm able to to vary the implementation of the
record()
and
playback()
methods independent of their interface. The astute reader may recognize this as a job for the
Bridge pattern
.
So, in the end, isn't
<replaced-method>
just a dependency-injected way of applying the Bridge pattern?
随机文章:
An Introduction to Aspect-Oriented Programming with the Spring Framework,
2004-11-23
AOP概念
2004-05-17
了解AOP(第一部分)
2004-05-14
Aspect-oriented programming with the Spring framework
2004-05-14
Spring 框架介绍
2004-05-12
收藏到:
Del.icio.us
引用地址:
发表于
13:38
|
编辑
|
继续话题
|
转发
|
推荐
发表评论
原始快照:web.archive.org