Skip to content

Commit 9bc0963

Browse files
committed
Recommend ObjectProvider as alternative to @lazy for optional dependencies
Closes gh-27649
1 parent 39be572 commit 9bc0963

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

spring-context/src/main/java/org/springframework/context/annotation/Lazy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -47,6 +47,11 @@
4747
* or {@link javax.inject.Inject}: In that context, it leads to the creation of a
4848
* lazy-resolution proxy for all affected dependencies, as an alternative to using
4949
* {@link org.springframework.beans.factory.ObjectFactory} or {@link javax.inject.Provider}.
50+
* Please note that such a lazy-resolution proxy will always be injected; if the target
51+
* dependency does not exist, you will only be able to find out through an exception on
52+
* invocation. As a consequence, such an injection point results in unintuitive behavior
53+
* for optional dependencies. For a programmatic equivalent, allowing for lazy references
54+
* with more sophistication, consider {@link org.springframework.beans.factory.ObjectProvider}.
5055
*
5156
* @author Chris Beams
5257
* @author Juergen Hoeller

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,7 +3039,7 @@ constructor or setter argument or autowired field) as `ObjectFactory<MyTargetBea
30393039
allowing for a `getObject()` call to retrieve the current instance on demand every
30403040
time it is needed -- without holding on to the instance or storing it separately.
30413041
3042-
As an extended variant, you may declare `ObjectProvider<MyTargetBean>`, which delivers
3042+
As an extended variant, you may declare `ObjectProvider<MyTargetBean>` which delivers
30433043
several additional access variants, including `getIfAvailable` and `getIfUnique`.
30443044
30453045
The JSR-330 variant of this is called `Provider` and is used with a `Provider<MyTargetBean>`
@@ -6675,9 +6675,11 @@ factory method and other bean definition properties, such as a qualifier value t
66756675
the `@Qualifier` annotation. Other method-level annotations that can be specified are
66766676
`@Scope`, `@Lazy`, and custom qualifier annotations.
66776677

6678-
TIP: In addition to its role for component initialization, you can also place the `@Lazy` annotation
6679-
on injection points marked with `@Autowired` or `@Inject`. In this context, it
6680-
leads to the injection of a lazy-resolution proxy.
6678+
TIP: In addition to its role for component initialization, you can also place the `@Lazy`
6679+
annotation on injection points marked with `@Autowired` or `@Inject`. In this context,
6680+
it leads to the injection of a lazy-resolution proxy. However, such a proxy approach
6681+
is rather limited. For sophisticated lazy interactions, in particular in combination
6682+
with optional dependencies, we recommend `ObjectProvider<MyTargetBean>` instead.
66816683

66826684
Autowired fields and methods are supported, as previously discussed, with additional
66836685
support for autowiring of `@Bean` methods. The following example shows how to do so:

0 commit comments

Comments
 (0)