Skip to content

Commit 2109db0

Browse files
committed
Stronger warning about lookup methods not working with @bean
Includes an updated CGLIB AOP proxy note on constructor invocations. Issue: SPR-13108 Issue: SPR-13103
1 parent 6f7ad3b commit 2109db0

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
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/index.adoc

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,15 +3327,17 @@ overrides the method.
33273327

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

33413343
Looking at the `CommandManager` class in the previous code snippet, you see that the
@@ -15610,13 +15612,10 @@ so. However, there are some issues to consider:
1561015612
CGLIB classes are repackaged under org.springframework and included directly in the
1561115613
spring-core JAR. This means that CGLIB-based proxy support 'just works' in the same
1561215614
way that JDK dynamic proxies always have.
15613-
* The constructor of your proxied object will be called twice. This is a natural
15614-
consequence of the CGLIB proxy model whereby a subclass is generated for each proxied
15615-
object. For each proxied instance, two objects are created: the actual proxied object
15616-
and an instance of the subclass that implements the advice. This behavior is not
15617-
exhibited when using JDK proxies. Usually, calling the constructor of the proxied type
15618-
twice, is not an issue, as there are usually only assignments taking place and no real
15619-
logic is implemented in the constructor.
15615+
* As of Spring 4.0, the constructor of your proxied object will NOT be called twice
15616+
anymore since the CGLIB proxy instance will be created via Objenesis. Only if your
15617+
JVM does not allow for constructor bypassing, you might see double invocations and
15618+
corresponding debug log entries from Spring's AOP support.
1562015619

1562115620
To force the use of CGLIB proxies set the value of the `proxy-target-class` attribute of
1562215621
the `<aop:config>` element to true:

0 commit comments

Comments
 (0)