Skip to content

Commit 69116c2

Browse files
committed
Documentation for InjectionPoint argument on @bean method
Issue: SPR-14797
1 parent a29188a commit 69116c2

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/asciidoc/core-beans.adoc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,7 +3980,6 @@ Consult the `PropertyPlaceholderConfigurer` javadocs for more information.
39803980

39813981
[TIP]
39823982
====
3983-
39843983
You can use the `PropertyPlaceholderConfigurer` to substitute class names, which is
39853984
sometimes useful when you have to pick a particular implementation class at runtime. For
39863985
example:
@@ -4366,7 +4365,6 @@ the `@Order` or standard `@Priority` annotation if you want items in the array o
43664365
to be sorted into a specific order.
43674366
====
43684367

4369-
43704368
Even typed Maps can be autowired as long as the expected key type is `String`. The Map
43714369
values will contain all beans of the expected type, and the keys will contain the
43724370
corresponding bean names:
@@ -4633,7 +4631,6 @@ be injected into a `Set<MovieCatalog>` annotated with `@Qualifier("action")`.
46334631

46344632
[TIP]
46354633
====
4636-
46374634
If you intend to express annotation-driven injection by name, do not primarily use
46384635
`@Autowired`, even if is technically capable of referring to a bean name through
46394636
`@Qualifier` values. Instead, use the JSR-250 `@Resource` annotation, which is
@@ -5320,7 +5317,6 @@ The following is an alternative using XML
53205317

53215318
[TIP]
53225319
====
5323-
53245320
The use of `<context:component-scan>` implicitly enables the functionality of
53255321
`<context:annotation-config>`. There is usually no need to include the
53265322
`<context:annotation-config>` element when using `<context:component-scan>`.
@@ -5486,7 +5482,6 @@ support for autowiring of `@Bean` methods:
54865482
}
54875483
54885484
// use of a custom qualifier and autowiring of method parameters
5489-
54905485
@Bean
54915486
protected TestBean protectedInstance(
54925487
@Qualifier("public") TestBean spouse,
@@ -5517,7 +5512,30 @@ defines the value of the property through the notation `#{ <expression> }`. For
55175512
annotations, an expression resolver is preconfigured to look for bean names when
55185513
resolving expression text.
55195514

5520-
The `@Bean` methods in a Spring component are processed differently than their
5515+
As of Spring Framework 4.3, you may also declare a factory method parameter of type
5516+
`InjectionPoint` (or its more specific subclass `DependencyDescriptor`) in order to
5517+
access the requesting injection point that triggers the creation of the current bean.
5518+
Note that this will only apply to the actual creation of bean instances, not to the
5519+
injection of existing instances. As a consequence, this feature makes most sense for
5520+
beans of prototype scope. For other scopes, the factory method will only ever see the
5521+
injection point which triggered the creation of a new bean instance in the given scope:
5522+
for example, the dependency that triggered the creation of a lazy singleton bean.
5523+
Use the provided injection point metadata with semantic care in such scenarios.
5524+
5525+
[source,java,indent=0]
5526+
[subs="verbatim,quotes"]
5527+
----
5528+
@Component
5529+
public class FactoryMethodComponent {
5530+
5531+
@Bean @Scope("prototype")
5532+
public TestBean prototypeInstance(InjectionPoint injectionPoint) {
5533+
return new TestBean("prototypeInstance for " + injectionPoint.getMember());
5534+
}
5535+
}
5536+
----
5537+
5538+
The `@Bean` methods in a regular Spring component are processed differently than their
55215539
counterparts inside a Spring `@Configuration` class. The difference is that `@Component`
55225540
classes are not enhanced with CGLIB to intercept the invocation of methods and fields.
55235541
CGLIB proxying is the means by which invoking methods or fields within `@Bean` methods
@@ -6149,7 +6167,6 @@ To enable component scanning, just annotate your `@Configuration` class as follo
61496167

61506168
[TIP]
61516169
====
6152-
61536170
Experienced Spring users will be familiar with the XML declaration equivalent from
61546171
Spring's `context:` namespace
61556172
@@ -6440,7 +6457,6 @@ method directly during construction:
64406457

64416458
[TIP]
64426459
====
6443-
64446460
When you work directly in Java, you can do anything you like with your objects and do
64456461
not always need to rely on the container lifecycle!
64466462
====

0 commit comments

Comments
 (0)