Skip to content

Commit 642fbfb

Browse files
committed
@lookup reference documentation
Issue: SPR-14765 (cherry picked from commit 684d6ab)
1 parent c98cdd4 commit 642fbfb

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

src/asciidoc/core-beans.adoc

+53-13
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,6 @@ overrides the method.
21032103
in particular not with `@Bean` methods in configuration classes, since the container
21042104
is not in charge of creating the instance in that case and therefore cannot create
21052105
a runtime-generated subclass on the fly.
2106-
* Finally, objects that have been the target of method injection cannot be serialized.
21072106
====
21082107

21092108
Looking at the `CommandManager` class in the previous code snippet, you see that the
@@ -2150,30 +2149,72 @@ the original class. For example:
21502149
[subs="verbatim,quotes"]
21512150
----
21522151
<!-- a stateful bean deployed as a prototype (non-singleton) -->
2153-
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
2152+
<bean id="myCommand" class="fiona.apple.AsyncCommand" scope="prototype">
21542153
<!-- inject dependencies here as required -->
21552154
</bean>
21562155
21572156
<!-- commandProcessor uses statefulCommandHelper -->
21582157
<bean id="commandManager" class="fiona.apple.CommandManager">
2159-
<lookup-method name="createCommand" bean="command"/>
2158+
<lookup-method name="createCommand" bean="myCommand"/>
21602159
</bean>
21612160
----
21622161

21632162
The bean identified as __commandManager__ calls its own method `createCommand()`
2164-
whenever it needs a new instance of the __command__ bean. You must be careful to deploy
2165-
the `command` bean as a prototype, if that is actually what is needed. If it is deployed
2166-
as a <<beans-factory-scopes-singleton,singleton>>, the same instance of the `command`
2163+
whenever it needs a new instance of the __myCommand__ bean. You must be careful to deploy
2164+
the `myCommand` bean as a prototype, if that is actually what is needed. If it is
2165+
as a <<beans-factory-scopes-singleton,singleton>>, the same instance of the `myCommand`
21672166
bean is returned each time.
21682167

2168+
Alternatively, within the annotation-based component model, you may declare a lookup
2169+
method through the `@Lookup` annotation:
2170+
2171+
[source,java,indent=0]
2172+
[subs="verbatim,quotes"]
2173+
----
2174+
public abstract class CommandManager {
2175+
2176+
public Object process(Object commandState) {
2177+
Command command = createCommand();
2178+
command.setState(commandState);
2179+
return command.execute();
2180+
}
2181+
2182+
@Lookup("myCommand")
2183+
protected abstract Command createCommand();
2184+
}
2185+
----
2186+
2187+
Or, more idiomatically, you may rely on the target bean getting resolved against the
2188+
declared return type of the lookup method:
2189+
2190+
[source,java,indent=0]
2191+
[subs="verbatim,quotes"]
2192+
----
2193+
public abstract class CommandManager {
2194+
2195+
public Object process(Object commandState) {
2196+
MyCommand command = createCommand();
2197+
command.setState(commandState);
2198+
return command.execute();
2199+
}
2200+
2201+
@Lookup
2202+
protected abstract MyCommand createCommand();
2203+
}
2204+
----
2205+
2206+
Note that you will typically declare such annotated lookup methods with a concrete
2207+
stub implementation, in order for them to be compatible with Spring's component
2208+
scanning rules where abstract classes get ignored by default. This limitation does not
2209+
apply in case of explicitly registered or explicitly imported bean classes.
2210+
21692211
[TIP]
21702212
====
2213+
Another way of accessing differently scoped target beans is an `ObjectFactory`/
2214+
`Provider` injection point. Check out <<beans-factory-scopes-other-injection>>.
2215+
21712216
The interested reader may also find the `ServiceLocatorFactoryBean` (in the
2172-
`org.springframework.beans.factory.config` package) to be of use. The approach used in
2173-
ServiceLocatorFactoryBean is similar to that of another utility class,
2174-
`ObjectFactoryCreatingFactoryBean`, but it allows you to specify your own lookup
2175-
interface as opposed to a Spring-specific lookup interface. Consult the javadocs of
2176-
these classes for additional information.
2217+
`org.springframework.beans.factory.config` package) to be of use.
21772218
====
21782219

21792220

@@ -6625,10 +6666,9 @@ type of configuration provides a natural means for implementing this pattern.
66256666
public Object process(Object commandState) {
66266667
// grab a new instance of the appropriate Command interface
66276668
Command command = createCommand();
6628-
66296669
// set the state on the (hopefully brand new) Command instance
66306670
command.setState(commandState);
6631-
return command.execute();
6671+
return command.execute();
66326672
}
66336673
66346674
// okay... but where is the implementation of this method?

0 commit comments

Comments
 (0)