Skip to content

Commit 4eea675

Browse files
committed
Stronger warning about lookup methods not working with @bean
Issue: SPR-13108
1 parent e97506b commit 4eea675

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/Lookup.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,12 +38,15 @@
3838
* container to fill them in at runtime. In both cases, the container will generate
3939
* runtime subclasses of the method's containing class via CGLIB, which is why such
4040
* lookup methods can only work on beans that the container instantiates through
41-
* regular constructors (i.e. lookup methods cannot get replaced on beans returned
42-
* from factory methods where we can't dynamically provide a subclass for them).
41+
* regular constructors: i.e. lookup methods cannot get replaced on beans returned
42+
* from factory methods where we cannot dynamically provide a subclass for them.
4343
*
44-
* <p>Note: When used with component scanning or any other mechanism that filters
45-
* out abstract beans, provide stub implementations of your lookup methods to be
46-
* able to declare them as concrete classes.
44+
* <p><b>Concrete limitations in typical Spring configuration scenarios:</b>
45+
* When used with component scanning or any other mechanism that filters out abstract
46+
* beans, provide stub implementations of your lookup methods to be able to declare
47+
* them as concrete classes. And please remember that lookup methods won't work on
48+
* beans returned from {@code @Bean} methods in configuration classes; you'll have
49+
* to resort to {@code @Inject Provider&lt;TargetBean&gt;} or the like instead.
4750
*
4851
* @author Juergen Hoeller
4952
* @since 4.1

src/asciidoc/core-beans.adoc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,15 +2032,17 @@ overrides the method.
20322032

20332033
[NOTE]
20342034
====
2035-
For this dynamic subclassing to work, the class that the Spring container will subclass
2036-
cannot be `final`, and the method to be overridden cannot be `final` either. Also,
2037-
testing a class that has an `abstract` method requires you to subclass the class
2038-
yourself and to supply a stub implementation of the `abstract` method. Finally, objects
2039-
that have been the target of method injection cannot be serialized. As of Spring 3.2 it
2040-
is no longer necessary to add CGLIB to your classpath, because CGLIB classes are
2041-
repackaged under org.springframework and distributed within the spring-core JAR. This is
2042-
done both for convenience as well as to avoid potential conflicts with other projects
2043-
that use differing versions of CGLIB.
2035+
* For this dynamic subclassing to work, the class that the Spring bean container will
2036+
subclass cannot be `final`, and the method to be overridden cannot be `final` either.
2037+
* Unit-testing a class that has an `abstract` method requires you to subclass the class
2038+
yourself and to supply a stub implementation of the `abstract` method.
2039+
* Concrete methods are also necessary for component scanning which requires concrete
2040+
classes to pick up.
2041+
* A further key limitation is that lookup methods won't work with factory methods and
2042+
in particular not with `@Bean` methods in configuration classes, since the container
2043+
is not in charge of creating the instance in that case and therefore cannot create
2044+
a runtime-generated subclass on the fly.
2045+
* Finally, objects that have been the target of method injection cannot be serialized.
20442046
====
20452047

20462048
Looking at the `CommandManager` class in the previous code snippet, you see that the
@@ -2116,7 +2118,7 @@ these classes for additional information.
21162118

21172119
[[beans-factory-arbitrary-method-replacement]]
21182120
==== Arbitrary method replacement
2119-
A less useful form of method injection than lookup method Injection is the ability to
2121+
A less useful form of method injection than lookup method injection is the ability to
21202122
replace arbitrary methods in a managed bean with another method implementation. Users
21212123
may safely skip the rest of this section until the functionality is actually needed.
21222124

0 commit comments

Comments
 (0)