@ -224,8 +224,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
@@ -224,8 +224,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
@ -568,8 +568,9 @@ is a convenience mechanism that sets up a <<core.adoc#beans-factory-placeholderc
@@ -568,8 +568,9 @@ is a convenience mechanism that sets up a <<core.adoc#beans-factory-placeholderc
This element activates the Spring infrastructure to detect annotations in bean classes:
* Spring's <<core.adoc#beans-factory-metadata, `@Configuration`>> model
* <<core.adoc#beans-annotation-config, `@Autowired`/`@Inject`>> and `@Value`
* JSR-250's `@Resource`, `@PostConstruct` and `@PreDestroy` (if available)
* <<core.adoc#beans-annotation-config, `@Autowired`/`@Inject`>>, `@Value`, and `@Lookup`
* JSR-250's `@Resource`, `@PostConstruct`, and `@PreDestroy` (if available)
* JAX-WS's `@WebServiceRef` and EJB 3's `@EJB` (if available)
* JPA's `@PersistenceContext` and `@PersistenceUnit` (if available)
@ -682,8 +683,7 @@ XML extension (a custom XML element) that lets us configure objects of the type
@@ -682,8 +683,7 @@ XML extension (a custom XML element) that lets us configure objects of the type
`SimpleDateFormat` (from the `java.text` package). When we are done,
we will be able to define bean definitions of type `SimpleDateFormat` as follows:
Notice that there is nothing special about this class. It is a POJO that
has no dependencies on container specific interfaces, base classes or annotations.
has no dependencies on container specific interfaces, base classes, or annotations.
[[beans-factory-ctor-arguments-resolution]]
===== Constructor Argument Resolution
@ -976,10 +974,10 @@ being instantiated. Consider the following class:
@@ -976,10 +974,10 @@ being instantiated. Consider the following class:
class ThingOne(thingTwo: ThingTwo, thingThree: ThingThree)
----
Assuming that `ThingTwo` and `ThingThree` classes are not related by inheritance, no potential
ambiguity exists. Thus, the following configuration works fine, and you do not need to specify
the constructor argument indexes or types explicitly in the `<constructor-arg/>`
element.
Assuming that the `ThingTwo` and `ThingThree` classes are not related by inheritance, no
potential ambiguity exists. Thus, the following configuration works fine, and you do not
need to specify the constructor argument indexes or types explicitly in the
`<constructor-arg/>` element.
[source,xml,indent=0,subs="verbatim,quotes"]
----
@ -1008,10 +1006,10 @@ by type without help. Consider the following class:
@@ -1008,10 +1006,10 @@ by type without help. Consider the following class:
public class ExampleBean {
// Number of years to calculate the Ultimate Answer
private int years;
private final int years;
// The Answer to Life, the Universe, and Everything
private String ultimateAnswer;
private final String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
@ -1033,7 +1031,7 @@ by type without help. Consider the following class:
@@ -1033,7 +1031,7 @@ by type without help. Consider the following class:
.[[beans-factory-ctor-arguments-type]]Constructor argument type matching
--
In the preceding scenario, the container can use type matching with simple types if
you explicitly specify the type of the constructor argument by using the `type` attribute.
you explicitly specify the type of the constructor argument by using the `type` attribute,
as the following example shows:
[source,xml,indent=0,subs="verbatim,quotes"]
@ -1253,7 +1251,8 @@ visibility of some configuration issues is why `ApplicationContext` implementati
@@ -1253,7 +1251,8 @@ visibility of some configuration issues is why `ApplicationContext` implementati
default pre-instantiate singleton beans. At the cost of some upfront time and memory to
create these beans before they are actually needed, you discover configuration issues
when the `ApplicationContext` is created, not later. You can still override this default
behavior so that singleton beans initialize lazily, rather than being pre-instantiated.
behavior so that singleton beans initialize lazily, rather than being eagerly
pre-instantiated.
If no circular dependencies exist, when one or more collaborating beans are being
injected into a dependent bean, each collaborating bean is totally configured prior
@ -1980,8 +1979,7 @@ then nested `constructor-arg` elements.
@@ -1980,8 +1979,7 @@ then nested `constructor-arg` elements.
The following example uses the `c:` namespace to do the same thing as the from
@ -3409,16 +3407,10 @@ The `org.springframework.beans.factory.InitializingBean` interface lets a bean
@@ -3409,16 +3407,10 @@ The `org.springframework.beans.factory.InitializingBean` interface lets a bean
perform initialization work after the container has set all necessary properties on the
bean. The `InitializingBean` interface specifies a single method:
We recommend that you do not use the `InitializingBean` interface, because it
unnecessarily couples the code to Spring. Alternatively, we suggest using
@ -3428,8 +3420,7 @@ you can use the `init-method` attribute to specify the name of the method that h
@@ -3428,8 +3420,7 @@ you can use the `init-method` attribute to specify the name of the method that h
no-argument signature. With Java configuration, you can use the `initMethod` attribute of
`@Bean`. See <<beans-java-lifecycle-callbacks>>. Consider the following example:
@ -3495,16 +3486,10 @@ Implementing the `org.springframework.beans.factory.DisposableBean` interface le
@@ -3495,16 +3486,10 @@ Implementing the `org.springframework.beans.factory.DisposableBean` interface le
bean get a callback when the container that contains it is destroyed. The
`DisposableBean` interface specifies a single method:
@ -3727,18 +3711,6 @@ lifecycle requirements (such as starting and stopping some background process):
@@ -3727,18 +3711,6 @@ lifecycle requirements (such as starting and stopping some background process):
Any Spring-managed object may implement the `Lifecycle` interface. Then, when the
`ApplicationContext` itself receives start and stop signals (for example, for a stop/restart
@ -3746,8 +3718,7 @@ scenario at runtime), it cascades those calls to all `Lifecycle` implementations
@@ -3746,8 +3718,7 @@ scenario at runtime), it cascades those calls to all `Lifecycle` implementations
defined within that context. It does this by delegating to a `LifecycleProcessor`, shown
Notice that the `LifecycleProcessor` is itself an extension of the `Lifecycle`
interface. It also adds two other methods for reacting to the context being refreshed
@ -3792,27 +3753,17 @@ prior to objects of another type. In those cases, the `SmartLifecycle` interface
@@ -3792,27 +3753,17 @@ prior to objects of another type. In those cases, the `SmartLifecycle` interface
another option, namely the `getPhase()` method as defined on its super-interface,
`Phased`. The following listing shows the definition of the `Phased` interface:
public interface SmartLifecycle extends Lifecycle, Phased {
@ -3821,16 +3772,6 @@ The following listing shows the definition of the `SmartLifecycle` interface:
@@ -3821,16 +3772,6 @@ The following listing shows the definition of the `SmartLifecycle` interface:
When starting, the objects with the lowest phase start first. When stopping, the
reverse order is followed. Therefore, an object that implements `SmartLifecycle` and
@ -3942,23 +3883,13 @@ When an `ApplicationContext` creates an object instance that implements the
@@ -3942,23 +3883,13 @@ When an `ApplicationContext` creates an object instance that implements the
with a reference to that `ApplicationContext`. The following listing shows the definition
fun setApplicationContext(applicationContext: ApplicationContext)
}
----
Thus, beans can programmatically manipulate the `ApplicationContext` that created them,
through the `ApplicationContext` interface or by casting the reference to a known
@ -3987,23 +3918,13 @@ When an `ApplicationContext` creates a class that implements the
@@ -3987,23 +3918,13 @@ When an `ApplicationContext` creates a class that implements the
a reference to the name defined in its associated object definition. The following listing
shows the definition of the BeanNameAware interface:
==== Example: The `RequiredAnnotationBeanPostProcessor`
[[beans-factory-extension-bpp-examples-aabpp]]
==== Example: The `AutowiredAnnotationBeanPostProcessor`
Using callback interfaces or annotations in conjunction with a custom
`BeanPostProcessor` implementation is a common means of extending the Spring IoC
container. An example is Spring's `RequiredAnnotationBeanPostProcessor` -- a
`BeanPostProcessor` implementation that ships with the Spring distribution and that ensures
that JavaBean properties on beans that are marked with an (arbitrary) annotation are
actually (configured to be) dependency-injected with a value.
Using callback interfaces or annotations in conjunction with a custom `BeanPostProcessor`
implementation is a common means of extending the Spring IoC container. An example is
Spring's `AutowiredAnnotationBeanPostProcessor` -- a `BeanPostProcessor` implementation
that ships with the Spring distribution and autowires annotated fields, setter methods,
and arbitrary config methods.
@ -4673,7 +4593,7 @@ An alternative to XML setup is provided by annotation-based configuration, which
@@ -4673,7 +4593,7 @@ An alternative to XML setup is provided by annotation-based configuration, which
the bytecode metadata for wiring up components instead of angle-bracket declarations.
Instead of using XML to describe a bean wiring, the developer moves the configuration
into the component class itself by using annotations on the relevant class, method, or
field declaration. As mentioned in <<beans-factory-extension-bpp-examples-rabpp>>, using
field declaration. As mentioned in <<beans-factory-extension-bpp-examples-aabpp>>, using
a `BeanPostProcessor` in conjunction with annotations is a common means of extending the
Spring IoC container. For example, Spring 2.0 introduced the possibility of enforcing
required properties with the <<beans-required-annotation,`@Required`>> annotation. Spring
@ -4692,8 +4612,8 @@ Annotation injection is performed before XML injection. Thus, the XML configurat
@@ -4692,8 +4612,8 @@ Annotation injection is performed before XML injection. Thus, the XML configurat
overrides the annotations for properties wired through both approaches.
====
As always, you can register them as individual bean definitions, but they can also be
implicitly registered by including the following tag in an XML-based Spring
As always, you can register the post-processors as individual bean definitions, but they
can also be implicitly registered by including the following tag in an XML-based Spring
configuration (notice the inclusion of the `context` namespace):
[source,xml,indent=0,subs="verbatim,quotes"]
@ -4712,12 +4632,13 @@ configuration (notice the inclusion of the `context` namespace):
@@ -4712,12 +4632,13 @@ configuration (notice the inclusion of the `context` namespace):
</beans>
----
(The implicitly registered post-processors include
This annotation indicates that the affected bean property must be populated at
configuration time, through an explicit property value in a bean definition or through
autowiring. The container throws an exception if the affected bean property has not been
@ -4772,11 +4692,18 @@ instances or the like later on. We still recommend that you put assertions into
@@ -4772,11 +4692,18 @@ instances or the like later on. We still recommend that you put assertions into
bean class itself (for example, into an init method). Doing so enforces those required
references and values even when you use the class outside of a container.
[TIP]
====
The {api-spring-framework}/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.html[`RequiredAnnotationBeanPostProcessor`]
must be registered as a bean to enable support for the `@Required` annotation.
====
[NOTE]
====
The `@Required` annotation is formally deprecated as of Spring Framework 5.1, in favor
of using constructor injection for required settings (or a custom implementation of
`InitializingBean.afterPropertiesSet()` along with bean property setter methods).
The `@Required` annotation and `RequiredAnnotationBeanPostProcessor` are formally
deprecated as of Spring Framework 5.1, in favor of using constructor injection for
required settings (or a custom implementation of `InitializingBean.afterPropertiesSet()`
or a custom `@PostConstruct` method along with bean property setter methods).
====
@ -5001,6 +4928,7 @@ The same applies for typed collections, as the following example shows:
@@ -5001,6 +4928,7 @@ The same applies for typed collections, as the following example shows:
}
----
[[beans-factory-ordered]]
[TIP]
====
Your target beans can implement the `org.springframework.core.Ordered` interface or use
@ -7197,10 +7125,10 @@ metadata is provided per-instance rather than per-class.
@@ -7197,10 +7125,10 @@ metadata is provided per-instance rather than per-class.
While classpath scanning is very fast, it is possible to improve the startup performance
of large applications by creating a static list of candidates at compilation time. In this
mode, all modules that are target of component scan must use this mechanism.
mode, all modules that are targets of component scanning must use this mechanism.
NOTE: Your existing `@ComponentScan` or `<context:component-scan` directives must stay as
is to request the context to scan candidates in certain packages. When the
NOTE: Your existing `@ComponentScan` or `<context:component-scan/>` directives must remain
unchanged to request the context to scan candidates in certain packages. When the
`ApplicationContext` detects such an index, it automatically uses it rather than scanning
the classpath.
@ -7229,12 +7157,10 @@ configuration, as shown in the following example:
@@ -7229,12 +7157,10 @@ configuration, as shown in the following example:
@ -7242,19 +7168,20 @@ configuration, as shown in the following example:
@@ -7242,19 +7168,20 @@ configuration, as shown in the following example:
}
----
That process generates a `META-INF/spring.components` file that is
included in the jar file.
The `spring-context-indexer` artifact generates a `META-INF/spring.components` file that
is included in the jar file.
NOTE: When working with this mode in your IDE, the `spring-context-indexer` must be
registered as an annotation processor to make sure the index is up-to-date when
candidate components are updated.
TIP: The index is enabled automatically when a `META-INF/spring.components` is found
TIP: The index is enabled automatically when a `META-INF/spring.components` file is found
on the classpath. If an index is partially available for some libraries (or use cases)
but could not be built for the whole application, you can fallback to a regular classpath
arrangement (as though no index was present at all) by setting `spring.index.ignore` to
`true`, either as a system property or in a `spring.properties` file at the root of the
classpath.
but could not be built for the whole application, you can fall back to a regular classpath
arrangement (as though no index were present at all) by setting `spring.index.ignore` to
`true`, either as a JVM system property or in a `spring.properties` file at the root of
the classpath.
@ -7857,8 +7784,7 @@ To enable component scanning, you can annotate your `@Configuration` class as fo
@@ -7857,8 +7784,7 @@ To enable component scanning, you can annotate your `@Configuration` class as fo
Experienced Spring users may be familiar with the XML declaration equivalent from
Spring's `context:` namespace, shown in the following example: