@@ -2103,7 +2103,6 @@ overrides the method.
2103
2103
in particular not with `@Bean` methods in configuration classes, since the container
2104
2104
is not in charge of creating the instance in that case and therefore cannot create
2105
2105
a runtime-generated subclass on the fly.
2106
- * Finally, objects that have been the target of method injection cannot be serialized.
2107
2106
====
2108
2107
2109
2108
Looking at the `CommandManager` class in the previous code snippet, you see that the
@@ -2150,30 +2149,72 @@ the original class. For example:
2150
2149
[subs="verbatim,quotes"]
2151
2150
----
2152
2151
<!-- 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">
2154
2153
<!-- inject dependencies here as required -->
2155
2154
</bean>
2156
2155
2157
2156
<!-- commandProcessor uses statefulCommandHelper -->
2158
2157
<bean id="commandManager" class="fiona.apple.CommandManager">
2159
- <lookup-method name="createCommand" bean="command "/>
2158
+ <lookup-method name="createCommand" bean="myCommand "/>
2160
2159
</bean>
2161
2160
----
2162
2161
2163
2162
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 `
2167
2166
bean is returned each time.
2168
2167
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
+
2169
2211
[TIP]
2170
2212
====
2213
+ Another way of accessing differently scoped target beans is an `ObjectFactory`/
2214
+ `Provider` injection point. Check out <<beans-factory-scopes-other-injection>>.
2215
+
2171
2216
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.
2177
2218
====
2178
2219
2179
2220
@@ -2616,6 +2657,9 @@ constructor/setter argument or autowired field) as `ObjectFactory<MyTargetBean>`
2616
2657
allowing for a `getObject()` call to retrieve the current instance on demand every
2617
2658
time it is needed - without holding on to the instance or storing it separately.
2618
2659
2660
+ As an extended variant, you may declare `ObjectProvider<MyTargetBean>` which delivers
2661
+ several additional access variants, including `getIfAvailable` and `getIfUnique`.
2662
+
2619
2663
The JSR-330 variant of this is called `Provider`, used with a `Provider<MyTargetBean>`
2620
2664
declaration and a corresponding `get()` call for every retrieval attempt.
2621
2665
See <<beans-standard-annotations,here>> for more details on JSR-330 overall.
@@ -6591,10 +6635,9 @@ type of configuration provides a natural means for implementing this pattern.
6591
6635
public Object process(Object commandState) {
6592
6636
// grab a new instance of the appropriate Command interface
6593
6637
Command command = createCommand();
6594
-
6595
6638
// set the state on the (hopefully brand new) Command instance
6596
6639
command.setState(commandState);
6597
- return command.execute();
6640
+ return command.execute();
6598
6641
}
6599
6642
6600
6643
// okay... but where is the implementation of this method?
0 commit comments