@ -182,7 +182,7 @@ use Java-based configuration, you can add `@EnableSpringConfigured` to any
@@ -182,7 +182,7 @@ use Java-based configuration, you can add `@EnableSpringConfigured` to any
This chapter covers Spring's Ahead of Time (AOT) optimizations.
For AOT support specific to integration tests, see xref:testing/testcontext-framework/aot.adoc[Ahead of Time Support for Tests].
[[core.aot.introduction]]
[[aot.introduction]]
== Introduction to Ahead of Time Optimizations
Spring's support for AOT optimizations is meant to inspect an `ApplicationContext` at build time and apply decisions and discovery logic that usually happens at runtime.
@ -30,7 +30,7 @@ A Spring AOT processed application typically generates:
@@ -30,7 +30,7 @@ A Spring AOT processed application typically generates:
NOTE: At the moment, AOT is focused on allowing Spring applications to be deployed as native images using GraalVM.
We intend to support more JVM-based use cases in future generations.
[[core.aot.basics]]
[[aot.basics]]
== AOT engine overview
The entry point of the AOT engine for processing an `ApplicationContext` arrangement is `ApplicationContextAotGenerator`. It takes care of the following steps, based on a `GenericApplicationContext` that represents the application to optimize and a {api-spring-framework}/aot/generate/GenerationContext.html[`GenerationContext`]:
@ -46,7 +46,7 @@ The `RuntimeHints` instance can also be used to generate the relevant GraalVM na
@@ -46,7 +46,7 @@ The `RuntimeHints` instance can also be used to generate the relevant GraalVM na
Those steps are covered in greater detail in the sections below.
[[core.aot.refresh]]
[[aot.refresh]]
== Refresh for AOT Processing
Refresh for AOT processing is supported on all `GenericApplicationContext` implementations.
@ -54,13 +54,13 @@ An application context is created with any number of entry points, usually in th
@@ -54,13 +54,13 @@ An application context is created with any number of entry points, usually in th
Starting this application with the regular runtime involves a number of steps including classpath scanning, configuration class parsing, bean instantiation, and lifecycle callback handling.
Refresh for AOT processing only applies a subset of what happens with a xref:core/beans/introduction.adoc[regular `refresh`].
In this mode, xref:core/beans/factory-extension.adoc#beans-factory-extension-factory-postprocessors[`BeanFactoryPostProcessor` implementations] are invoked as usual.
This includes configuration class parsing, import selectors, classpath scanning, etc.
@ -76,7 +76,7 @@ This makes sure to create any proxy that will be required at runtime.
@@ -76,7 +76,7 @@ This makes sure to create any proxy that will be required at runtime.
Once this part completes, the `BeanFactory` contains the bean definitions that are necessary for the application to run. It does not trigger bean instantiation but allows the AOT engine to inspect the beans that will be created at runtime.
Components that want to participate in this step can implement the {api-spring-framework}/beans/factory/aot/BeanFactoryInitializationAotProcessor.html[`BeanFactoryInitializationAotProcessor`] interface.
@ -99,7 +99,7 @@ If such a bean is registered using an `@Bean` factory method, ensure the method
@@ -99,7 +99,7 @@ If such a bean is registered using an `@Bean` factory method, ensure the method
====
[[core.aot.bean-registration-contributions]]
[[aot.bean-registration-contributions]]
=== Bean Registration AOT Contributions
A core `BeanFactoryInitializationAotProcessor` implementation is responsible for collecting the necessary contributions for each candidate `BeanDefinition`.
@ -186,7 +186,7 @@ When a `datasource` instance is required, a `BeanInstanceSupplier` is called.
@@ -186,7 +186,7 @@ When a `datasource` instance is required, a `BeanInstanceSupplier` is called.
This supplier invokes the `dataSource()` method on the `dataSourceConfiguration` bean.
[[core.aot.hints]]
[[aot.hints]]
== Runtime Hints
Running an application as a native image requires additional information compared to a regular JVM runtime.
@ -210,14 +210,14 @@ For cases that the core container cannot infer, you can register such hints prog
@@ -210,14 +210,14 @@ For cases that the core container cannot infer, you can register such hints prog
A number of convenient annotations are also provided for common use cases.
[[core.aot.hints.import-runtime-hints]]
[[aot.hints.import-runtime-hints]]
=== `@ImportRuntimeHints`
`RuntimeHintsRegistrar` implementations allow you to get a callback to the `RuntimeHints` instance managed by the AOT engine.
Implementations of this interface can be registered using `@ImportRuntimeHints` on any Spring bean or `@Bean` factory method.
`RuntimeHintsRegistrar` implementations are detected and invoked at build time.
include-code::SpellCheckService[]
include-code::./SpellCheckService[]
If at all possible, `@ImportRuntimeHints` should be used as close as possible to the component that requires the hints.
This way, if the component is not contributed to the `BeanFactory`, the hints won't be contributed either.
@ -225,7 +225,7 @@ This way, if the component is not contributed to the `BeanFactory`, the hints wo
@@ -225,7 +225,7 @@ This way, if the component is not contributed to the `BeanFactory`, the hints wo
It is also possible to register an implementation statically by adding an entry in `META-INF/spring/aot.factories` with a key equal to the fully qualified name of the `RuntimeHintsRegistrar` interface.
[[core.aot.hints.reflective]]
[[aot.hints.reflective]]
=== `@Reflective`
{api-spring-framework}/aot/hint/annotation/Reflective.html[`@Reflective`] provides an idiomatic way to flag the need for reflection on an annotated element.
@ -239,7 +239,7 @@ Library authors can reuse this annotation for their own purposes.
@@ -239,7 +239,7 @@ Library authors can reuse this annotation for their own purposes.
If components other than Spring beans need to be processed, a `BeanFactoryInitializationAotProcessor` can detect the relevant types and use `ReflectiveRuntimeHintsRegistrar` to process them.
{api-spring-framework}/aot/hint/annotation/RegisterReflectionForBinding.html[`@RegisterReflectionForBinding`] is a specialization of `@Reflective` that registers the need for serializing arbitrary types.
@ -262,14 +262,14 @@ The following example registers `Account` for serialization.
@@ -262,14 +262,14 @@ The following example registers `Account` for serialization.
}
----
[[core.aot.hints.testing]]
[[aot.hints.testing]]
=== Testing Runtime Hints
Spring Core also ships `RuntimeHintsPredicates`, a utility for checking that existing hints match a particular use case.
This can be used in your own tests to validate that a `RuntimeHintsRegistrar` contains the expected results.
We can write a test for our `SpellCheckService` and ensure that we will be able to load a dictionary at runtime:
With `RuntimeHintsPredicates`, we can check for reflection, resource, serialization, or proxy generation hints.
This approach works well for unit tests but implies that the runtime behavior of a component is well known.
@ -281,11 +281,11 @@ For more targeted discovery and testing, Spring Framework ships a dedicated modu
@@ -281,11 +281,11 @@ For more targeted discovery and testing, Spring Framework ships a dedicated modu
This module contains the RuntimeHints Agent, a Java agent that records all method invocations that are related to runtime hints and helps you to assert that a given `RuntimeHints` instance covers all recorded invocations.
Let's consider a piece of infrastructure for which we'd like to test the hints we're contributing during the AOT processing phase.
include-code::SampleReflection[]
include-code::./SampleReflection[]
We can then write a unit test (no native compilation required) that checks our contributed hints:
Since version 2.0, Spring has featured a mechanism for adding schema-based extensions to the
basic Spring XML format for defining and configuring beans. This section covers
how to write your own custom XML bean definition parsers and
@ -39,7 +39,7 @@ through the basic steps of making a custom extension.)
@@ -39,7 +39,7 @@ through the basic steps of making a custom extension.)
[[core.appendix.xsd-custom-schema]]
[[xsd-custom-schema]]
== Authoring the Schema
Creating an XML configuration extension for use with Spring's IoC container starts with
@ -111,7 +111,7 @@ defined in the enumeration.
@@ -111,7 +111,7 @@ defined in the enumeration.
[[core.appendix.xsd-custom-namespacehandler]]
[[xsd-custom-namespacehandler]]
== Coding a `NamespaceHandler`
In addition to the schema, we need a `NamespaceHandler` to parse all elements of
@ -182,7 +182,7 @@ custom element, as we can see in the next step.
@@ -182,7 +182,7 @@ custom element, as we can see in the next step.
[[core.appendix.xsd-custom-parser]]
[[xsd-custom-parser]]
== Using `BeanDefinitionParser`
A `BeanDefinitionParser` is used if the `NamespaceHandler` encounters an XML
@ -272,7 +272,7 @@ is the extraction and setting of the bean definition's unique identifier.
@@ -272,7 +272,7 @@ is the extraction and setting of the bean definition's unique identifier.
[[core.appendix.xsd-custom-registration]]
[[xsd-custom-registration]]
== Registering the Handler and the Schema
The coding is finished. All that remains to be done is to make the Spring XML
@ -284,7 +284,7 @@ XML parsing infrastructure automatically picks up your new extension by consumin
@@ -284,7 +284,7 @@ XML parsing infrastructure automatically picks up your new extension by consumin
these special properties files, the formats of which are detailed in the next two sections.
The properties file called `spring.handlers` contains a mapping of XML Schema URIs to
@ -303,7 +303,7 @@ namespace extension and needs to exactly match exactly the value of the `targetN
@@ -303,7 +303,7 @@ namespace extension and needs to exactly match exactly the value of the `targetN
attribute, as specified in your custom XSD schema.
The properties file called `spring.schemas` contains a mapping of XML Schema locations
@ -327,7 +327,7 @@ the `NamespaceHandler` and `BeanDefinitionParser` classes on the classpath.
@@ -327,7 +327,7 @@ the `NamespaceHandler` and `BeanDefinitionParser` classes on the classpath.
[[core.appendix.xsd-custom-using]]
[[xsd-custom-using]]
== Using a Custom Extension in Your Spring XML Configuration
Using a custom extension that you yourself have implemented is no different from using
@ -361,13 +361,13 @@ in a Spring XML configuration file:
@@ -361,13 +361,13 @@ in a Spring XML configuration file:
[[core.appendix.xsd-custom-meat]]
[[xsd-custom-meat]]
== More Detailed Examples
This section presents some more detailed examples of custom XML extensions.
[[core.appendix.xsd-custom-custom-nested]]
[[xsd-custom-custom-nested]]
=== Nesting Custom Elements within Custom Elements
The example presented in this section shows how you to write the various artifacts required
This part of the appendix lists XML schemas related to the core container.
[[core.appendix.xsd-schemas-util]]
[[xsd-schemas-util]]
== The `util` Schema
As the name implies, the `util` tags deal with common, utility configuration
@ -30,7 +30,7 @@ correct schema so that the tags in the `util` namespace are available to you):
@@ -30,7 +30,7 @@ correct schema so that the tags in the `util` namespace are available to you):
----
[[core.appendix.xsd-schemas-util-constant]]
[[xsd-schemas-util-constant]]
=== Using `<util:constant/>`
Consider the following bean definition:
@ -63,7 +63,7 @@ developer's intent ("`inject this constant value`"), and it reads better:
@@ -63,7 +63,7 @@ developer's intent ("`inject this constant value`"), and it reads better:
</bean>
----
[[core.appendix.xsd-schemas-util-frfb]]
[[xsd-schemas-util-frfb]]
==== Setting a Bean Property or Constructor Argument from a Field Value
@ -175,7 +175,7 @@ Now consider the following setter of type `PersistenceContextType` and the corre
@@ -175,7 +175,7 @@ Now consider the following setter of type `PersistenceContextType` and the corre
----
[[core.appendix.xsd-schemas-util-property-path]]
[[xsd-schemas-util-property-path]]
=== Using `<util:property-path/>`
Consider the following example:
@ -222,7 +222,7 @@ The value of the `path` attribute of the `<property-path/>` element follows the
@@ -222,7 +222,7 @@ The value of the `path` attribute of the `<property-path/>` element follows the
`beanName.beanProperty`. In this case, it picks up the `age` property of the bean named
`testBean`. The value of that `age` property is `10`.
==== Using `<util:property-path/>` to Set a Bean Property or Constructor Argument
`PropertyPathFactoryBean` is a `FactoryBean` that evaluates a property path on a given
@ -297,7 +297,7 @@ for most use cases, but it can sometimes be useful. See the javadoc for more inf
@@ -297,7 +297,7 @@ for most use cases, but it can sometimes be useful. See the javadoc for more inf
this feature.
[[core.appendix.xsd-schemas-util-properties]]
[[xsd-schemas-util-properties]]
=== Using `<util:properties/>`
Consider the following example:
@ -323,7 +323,7 @@ The following example uses a `util:properties` element to make a more concise re
@@ -323,7 +323,7 @@ The following example uses a `util:properties` element to make a more concise re
----
[[core.appendix.xsd-schemas-util-list]]
[[xsd-schemas-util-list]]
=== Using `<util:list/>`
Consider the following example:
@ -378,7 +378,7 @@ following configuration:
@@ -378,7 +378,7 @@ following configuration:
If no `list-class` attribute is supplied, the container chooses a `List` implementation.
[[core.appendix.xsd-schemas-util-map]]
[[xsd-schemas-util-map]]
=== Using `<util:map/>`
Consider the following example:
@ -433,7 +433,7 @@ following configuration:
@@ -433,7 +433,7 @@ following configuration:
If no `'map-class'` attribute is supplied, the container chooses a `Map` implementation.
[[core.appendix.xsd-schemas-util-set]]
[[xsd-schemas-util-set]]
=== Using `<util:set/>`
Consider the following example:
@ -489,7 +489,7 @@ If no `set-class` attribute is supplied, the container chooses a `Set` implement
@@ -489,7 +489,7 @@ If no `set-class` attribute is supplied, the container chooses a `Set` implement
[[core.appendix.xsd-schemas-aop]]
[[xsd-schemas-aop]]
== The `aop` Schema
The `aop` tags deal with configuring all things AOP in Spring, including Spring's
@ -519,7 +519,7 @@ are available to you):
@@ -519,7 +519,7 @@ are available to you):
[[core.appendix.xsd-schemas-context]]
[[xsd-schemas-context]]
== The `context` Schema
The `context` tags deal with `ApplicationContext` configuration that relates to plumbing
@ -544,7 +544,7 @@ available to you:
@@ -544,7 +544,7 @@ available to you:
----
[[core.appendix.xsd-schemas-context-pphc]]
[[xsd-schemas-context-pphc]]
=== Using `<property-placeholder/>`
This element activates the replacement of `${...}` placeholders, which are resolved against a
@ -554,7 +554,7 @@ is a convenience mechanism that sets up a xref:core/beans/factory-extension.adoc
@@ -554,7 +554,7 @@ is a convenience mechanism that sets up a xref:core/beans/factory-extension.adoc
`PropertySourcesPlaceholderConfigurer` setup, you can explicitly define it as a bean yourself.
[[core.appendix.xsd-schemas-context-ac]]
[[xsd-schemas-context-ac]]
=== Using `<annotation-config/>`
This element activates the Spring infrastructure to detect annotations in bean classes:
@ -577,28 +577,28 @@ xref:integration/cache/annotations.adoc[caching annotations] need to be explicit
@@ -577,28 +577,28 @@ xref:integration/cache/annotations.adoc[caching annotations] need to be explicit
xref:integration/cache/annotations.adoc#cache-annotation-enable[enabled] as well.
This element is detailed in the section on xref:core/beans/annotation-config.adoc[annotation-based container configuration]
.
[[core.appendix.xsd-schemas-context-ltw]]
[[xsd-schemas-context-ltw]]
=== Using `<load-time-weaver/>`
This element is detailed in the section on xref:core/aop/using-aspectj.adoc#aop-aj-ltw[load-time weaving with AspectJ in the Spring Framework]
.
[[core.appendix.xsd-schemas-context-sc]]
[[xsd-schemas-context-sc]]
=== Using `<spring-configured/>`
This element is detailed in the section on xref:core/aop/using-aspectj.adoc#aop-atconfigurable[using AspectJ to dependency inject domain objects with Spring]
.
[[core.appendix.xsd-schemas-context-mbe]]
[[xsd-schemas-context-mbe]]
=== Using `<mbean-export/>`
This element is detailed in the section on xref:integration/jmx/naming.adoc#jmx-context-mbeanexport[configuring annotation-based MBean export]
@ -606,7 +606,7 @@ This element is detailed in the section on xref:integration/jmx/naming.adoc#jmx-
@@ -606,7 +606,7 @@ This element is detailed in the section on xref:integration/jmx/naming.adoc#jmx-
[[core.appendix.xsd-schemas-beans]]
[[xsd-schemas-beans]]
== The Beans Schema
Last but not least, we have the elements in the `beans` schema. These elements
This part of the appendix lists XML schemas related to integration technologies.
[[integration.appendix.xsd-schemas-jee]]
[[appendix.xsd-schemas-jee]]
=== The `jee` Schema
The `jee` elements deal with issues related to Jakarta EE (Enterprise Edition) configuration,
@ -40,7 +40,7 @@ correct schema so that the elements in the `jee` namespace are available to you:
@@ -40,7 +40,7 @@ correct schema so that the elements in the `jee` namespace are available to you:
==== `<jee:jndi-lookup/>` (with Single JNDI Environment Setting)
The following example shows how to use JNDI to look up an environment variable without
@ -99,7 +99,7 @@ The following example shows how to use JNDI to look up an environment variable w
@@ -99,7 +99,7 @@ The following example shows how to use JNDI to look up an environment variable w
The following example shows how to use JNDI to look up multiple environment variables
@ -133,7 +133,7 @@ The following example shows how to use JNDI to look up multiple environment vari
@@ -133,7 +133,7 @@ The following example shows how to use JNDI to look up multiple environment vari
Micrometer defines an https://micrometer.io/docs/observation[Observation concept that enables both Metrics and Traces] in applications.
@ -10,7 +10,7 @@ Spring Framework instruments various parts of its own codebase to publish observ
@@ -10,7 +10,7 @@ Spring Framework instruments various parts of its own codebase to publish observ
You can learn more about {docs-spring-boot}/html/actuator.html#actuator.metrics[configuring the observability infrastructure in Spring Boot].
[[integration.observability.list]]
[[observability.list]]
== List of produced Observations
Spring Framework instruments various features for observability.
@ -21,10 +21,10 @@ As outlined xref:integration/observability.adoc[at the beginning of this section
@@ -21,10 +21,10 @@ As outlined xref:integration/observability.adoc[at the beginning of this section
|Processing time for HTTP server exchanges at the Framework level
|===
@ -33,7 +33,7 @@ https://micrometer.io/docs/concepts#_naming_meters[to the format preferred by th
@@ -33,7 +33,7 @@ https://micrometer.io/docs/concepts#_naming_meters[to the format preferred by th
(Prometheus, Atlas, Graphite, InfluxDB...).
[[integration.observability.concepts]]
[[observability.concepts]]
== Micrometer Observation concepts
If you are not familiar with Micrometer Observation, here's a quick summary of the new concepts you should know about.
@ -49,7 +49,7 @@ If you are not familiar with Micrometer Observation, here's a quick summary of t
@@ -49,7 +49,7 @@ If you are not familiar with Micrometer Observation, here's a quick summary of t
* An `ObservationDocumentation` documents all observations in a particular domain, listing the expected key names and their meaning.
[[integration.observability.config]]
[[observability.config]]
== Configuring Observations
Global configuration options are available at the `ObservationRegistry#observationConfig()` level.
@ -59,33 +59,33 @@ Each instrumented component will provide two extension points:
@@ -59,33 +59,33 @@ Each instrumented component will provide two extension points:
* providing a custom `ObservationConvention` to change the default observation name and extracted `KeyValues`
[[integration.observability.config.conventions]]
[[observability.config.conventions]]
=== Using custom Observation conventions
Let's take the example of the Spring MVC "http.server.requests" metrics instrumentation with the `ServerHttpObservationFilter`.
This observation is using a `ServerRequestObservationConvention` with a `ServerRequestObservationContext`; custom conventions can be configured on the Servlet filter.
If you would like to customize the metadata produced with the observation, you can extend the `DefaultServerRequestObservationConvention` for your requirements:
You can also achieve similar goals using a custom `ObservationFilter` - adding or removing key values for an observation.
Filters do not replace the default convention and are used as a post-processing component.
include-code::ServerRequestObservationFilter[]
include-code::./ServerRequestObservationFilter[]
You can configure `ObservationFilter` instances on the `ObservationRegistry`.
[[integration.observability.http-server]]
[[observability.http-server]]
== HTTP Server instrumentation
HTTP server exchanges observations are created with the name `"http.server.requests"` for Servlet and Reactive applications.
[[integration.observability.http-server.servlet]]
[[observability.http-server.servlet]]
=== Servlet applications
Applications need to configure the `org.springframework.web.filter.ServerHttpObservationFilter` Servlet filter in their application.
@ -95,7 +95,7 @@ This will only record an observation as an error if the `Exception` has not been
@@ -95,7 +95,7 @@ This will only record an observation as an error if the `Exception` has not been
Typically, all exceptions handled by Spring MVC's `@ExceptionHandler` and xref:web/webmvc/mvc-ann-rest-exceptions.adoc[`ProblemDetail` support] will not be recorded with the observation.
You can, at any point during request processing, set the error field on the `ObservationContext` yourself:
include-code::UserController[]
include-code::./UserController[]
By default, the following `KeyValues` are created:
@ -118,7 +118,7 @@ By default, the following `KeyValues` are created:
@@ -118,7 +118,7 @@ By default, the following `KeyValues` are created:
Applications need to configure the `org.springframework.web.filter.reactive.ServerHttpObservationFilter` reactive `WebFilter` in their application.
@ -128,7 +128,7 @@ This will only record an observation as an error if the `Exception` has not been
@@ -128,7 +128,7 @@ This will only record an observation as an error if the `Exception` has not been
Typically, all exceptions handled by Spring WebFlux's `@ExceptionHandler` and xref:web/webflux/ann-rest-exceptions.adoc[`ProblemDetail` support] will not be recorded with the observation.
You can, at any point during request processing, set the error field on the `ObservationContext` yourself:
include-code::UserController[]
include-code::./UserController[]
By default, the following `KeyValues` are created:
@ -152,13 +152,13 @@ By default, the following `KeyValues` are created:
@@ -152,13 +152,13 @@ By default, the following `KeyValues` are created:
[[integration.observability.http-client]]
[[observability.http-client]]
== HTTP Client instrumentation
HTTP client exchanges observations are created with the name `"http.client.requests"` for blocking and reactive clients.
Unlike their server counterparts, the instrumentation is implemented directly in the client so the only required step is to configure an `ObservationRegistry` on the client.
Applications must configure an `ObservationRegistry` on `RestTemplate` instances to enable the instrumentation; without that, observations are "no-ops".
@ -187,7 +187,7 @@ Instrumentation is using the `org.springframework.http.client.observation.Client
@@ -187,7 +187,7 @@ Instrumentation is using the `org.springframework.http.client.observation.Client
Applications must configure an `ObservationRegistry` on the `WebClient` builder to enable the instrumentation; without that, observations are "no-ops".
@ -118,7 +118,7 @@ accessing the status of a response that represents an error (such as 401). If th
@@ -118,7 +118,7 @@ accessing the status of a response that represents an error (such as 401). If th
issue, switch to another HTTP client library.
NOTE: `RestTemplate` can be instrumented for observability, in order to produce metrics and traces.
See the xref:integration/observability.adoc#integration.observability.http-client.resttemplate[RestTemplate Observability support] section.
See the xref:integration/observability.adoc#http-client.resttemplate[RestTemplate Observability support] section.
@ -16,8 +16,8 @@ You can also use `WebClient.builder()` with further options:
@@ -16,8 +16,8 @@ You can also use `WebClient.builder()` with further options:
* `observationRegistry`: the registry to use for enabling xref:integration/observability.adoc#integration.observability.http-client.webclient[Observability support].
* `observationConvention`: xref:integration/observability.adoc#integration.observability.config[an optional, custom convention to extract metadata] for recorded observations.
* `observationRegistry`: the registry to use for enabling xref:integration/observability.adoc#http-client.webclient[Observability support].
* `observationConvention`: xref:integration/observability.adoc#config[an optional, custom convention to extract metadata] for recorded observations.