Using Method Injection
Besides Constructor and Setter Injection, another less frequently used DI feature that Spring provides is
Method Injection. Spring's Method Injection capabilities come in two loosely related forms, Lookup
Method Injection and Method Replacement. Lookup Method Injection provides another mechanism by
which a bean can obtain one of its dependencies, and Method Replacement allows you to replace the
implementation of any method on a bean arbitrarily, without having to change the original source code.
To provide these two features, Spring uses the dynamic bytecode enhancement capabilities of
CGLIB. If you want to use Lookup Method Injection or Method Replacement in your application, make
sure you have the CGLIB JAR file on your classpath.
Lookup Method Injection
Lookup Method Injection was added to Spring since version 1.1 to overcome the problems encountered
when a bean depends on another bean with a different life cycle--specifically, when a singleton depends
on a nonsingleton. In this situation, both Setter and Constructor Injection result in the singleton
maintaining a single instance of what should be a nonsingleton bean. In some cases, you will want to have
the singleton bean obtain a new instance of the nonsingleton every time it requires the bean in question.
Consider a scenario where there is a LockOpener class that provides the service of opening any locker.
The LockOpener class relies on a KeyHelper class for opening the locker, which was injected into
LockOpener. However, the design of KeyHelper class involves some internal states that make it not suitable
for reuse. Every time the openLock() method is called, a new KeyHelper instance is required. In this case,
LockOpener will be a singleton. However, if we inject the KeyHelper class using the normal mechanism, the
same instance of the KeyHelper class (that was instantiated when Spring performed the injection the first
time) will be reused. To make sure that a new instance of the KeyHelper instance was passed into the
openLock() method every time it was invoked, we need to use a Lookup Method Injection.
Typically, you can achieve this by having the singleton bean implement the
ApplicationContextAware interface (we will discuss this interface in next chapter). Then, using the
ApplicationContext instance, the singleton bean can look up a new instance of the nonsingleton
dependency every time it needs it. Lookup Method Injection allows the singleton bean to declare that it
requires a nonsingleton dependency and that it receive a new instance of the nonsingleton bean each
time it needs to interact with it, without needing to implement any Spring-specific interfaces.
Lookup Method Injection works by having your singleton declare a method, the lookup method,
which returns an instance of the nonsingleton bean. When you obtain a reference to the singleton in
your application, you are actually receiving a reference to a dynamically created subclass on which
Spring has implemented the lookup method. A typical implementation involves defining the lookup
method, and thus the bean class, as abstract. This prevents any strange errors from creeping in when
you forget to configure the Method Injection and you are working directly against the bean class with the
empty method implementation instead of the Spring-enhanced subclass. This topic is quite complex
and is best shown by example.
In this example, we create one nonsingleton bean and two singleton beans that both implement the
same interface. One of the singletons obtains an instance of the nonsingleton bean using "traditional"
Setter Injection; the other uses Method Injection. Listing 4-57 shows the MyHelper bean, which in our
example is the nonsingleton bean.
Listing 4-57. The MyHelper Bean
package com.apress.prospring3.ch4.mi;
public class MyHelper {
public void doSomethingHelpful() {
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home