@ -1024,7 +1024,7 @@ by type without help. Consider the following class:
class ExampleBean(
class ExampleBean(
private val years: Int, // Number of years to calculate the Ultimate Answer
private val years: Int, // Number of years to calculate the Ultimate Answer
private val ultimateAnswer: String// The Answer to Life, the Universe, and Everything
private val ultimateAnswer: String // The Answer to Life, the Universe, and Everything
)
)
----
----
@ -1608,7 +1608,7 @@ listings shows how to use the `parent` attribute:
----
----
<!-- in the parent context -->
<!-- in the parent context -->
<bean id="accountService" class="com.something.SimpleAccountService">
<bean id="accountService" class="com.something.SimpleAccountService">
<!-- insert dependencies as required as here -->
<!-- insert dependencies as required here -->
</bean>
</bean>
----
----
@ -1690,7 +1690,7 @@ respectively. The following example shows how to use them:
<property name="someMap">
<property name="someMap">
<map>
<map>
<entry key="an entry" value="just some string"/>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
<entry key="a ref" value-ref="myDataSource"/>
</map>
</map>
</property>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<!-- results in a setSomeSet(java.util.Set) call -->
@ -1834,7 +1834,7 @@ class SomeClass {
When the `accounts` property of the `something` bean is prepared for injection, the generics
When the `accounts` property of the `something` bean is prepared for injection, the generics
information about the element type of the strongly-typed `Map<String, Float>` is
information about the element type of the strongly-typed `Map<String, Float>` is
available by reflection. Thus, Spring's type conversion infrastructure recognizes the
available by reflection. Thus, Spring's type conversion infrastructure recognizes the
various value elements as being of type `Float`, and the string values (`9.99, 2.75`, and
various value elements as being of type `Float`, and the string values (`9.99` , ` 2.75`, and
`3.99`) are converted into an actual `Float` type.
`3.99`) are converted into an actual `Float` type.
@ -2028,7 +2028,7 @@ not commonly used since the plain order of declaration is usually sufficient the
In practice, the constructor resolution
In practice, the constructor resolution
<<beans-factory-ctor-arguments-resolution,mechanism>> is quite efficient in matching
<<beans-factory-ctor-arguments-resolution,mechanism>> is quite efficient in matching
arguments, so unless you really need to, we recommend using the name notation
arguments, so unless you really need to, we recommend using the name notation
through- out your configuration.
throughout your configuration.
[[beans-compound-property-names]]
[[beans-compound-property-names]]
@ -2503,13 +2503,13 @@ declared return type of the lookup method:
public abstract class CommandManager {
public abstract class CommandManager {
public Object process(Object commandState) {
public Object process(Object commandState) {
My Command command = createCommand();
Command command = createCommand();
command.setState(commandState);
command.setState(commandState);
return command.execute();
return command.execute();
}
}
@Lookup
@Lookup
protected abstract My Command createCommand();
protected abstract Command createCommand();
}
}
----
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
@ -2604,9 +2604,9 @@ interface provides the new method definition, as the following example shows:
.Kotlin
.Kotlin
----
----
/**
/**
* meant to be used to override the existing computeValue(String)
* meant to be used to override the existing computeValue(String)
* implementation in MyValueCalculator
* implementation in MyValueCalculator
*/
*/
class ReplacementComputeValue : MethodReplacer {
class ReplacementComputeValue : MethodReplacer {
override fun reimplement(obj: Any, method: Method, args: Array<out Any>): Any {
override fun reimplement(obj: Any, method: Method, args: Array<out Any>): Any {
@ -2806,7 +2806,7 @@ prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a
prototype-scoped bean into your singleton bean, because that injection occurs only
prototype-scoped bean into your singleton bean, because that injection occurs only
once, when the Spring container instantiates the singleton bean and resolves
once, when the Spring container instantiates the singleton bean and resolves
and injects its dependencies. If you need a new instance of a prototype bean at
and injects its dependencies. If you need a new instance of a prototype bean at
runtime more than once, see <<beans-factory-method-injection>>
runtime more than once, see <<beans-factory-method-injection>>.
@ -2984,7 +2984,7 @@ The Spring container creates a new instance of the `AppPreferences` bean by usin
`appPreferences` bean is scoped at the `ServletContext` level and stored as a regular
`appPreferences` bean is scoped at the `ServletContext` level and stored as a regular
`ServletContext` attribute. This is somewhat similar to a Spring singleton bean but
`ServletContext` attribute. This is somewhat similar to a Spring singleton bean but
differs in two important ways: It is a singleton per `ServletContext`, not per Spring
differs in two important ways: It is a singleton per `ServletContext`, not per Spring
'ApplicationContext' (for which there may be several in any given web application),
`ApplicationContext` (for which there may be several in any given web application),
and it is actually exposed and therefore visible as a `ServletContext` attribute.
and it is actually exposed and therefore visible as a `ServletContext` attribute.
When using annotation-driven components or Java configuration, you can use the
When using annotation-driven components or Java configuration, you can use the
@ -3100,7 +3100,7 @@ to the HTTP `Session`-scoped bean (`userPreferences`). The salient point here is
`userManager` bean is a singleton: it is instantiated exactly once per
`userManager` bean is a singleton: it is instantiated exactly once per
container, and its dependencies (in this case only one, the `userPreferences` bean) are
container, and its dependencies (in this case only one, the `userPreferences` bean) are
also injected only once. This means that the `userManager` bean operates only on the
also injected only once. This means that the `userManager` bean operates only on the
exact same `userPreferences` object (that is, the one with which it was originally injected.
exact same `userPreferences` object (that is, the one with which it was originally injected) .
This is not the behavior you want when injecting a shorter-lived scoped bean into a
This is not the behavior you want when injecting a shorter-lived scoped bean into a
longer-lived scoped bean (for example, injecting an HTTP `Session`-scoped collaborating
longer-lived scoped bean (for example, injecting an HTTP `Session`-scoped collaborating
@ -3928,7 +3928,7 @@ shows the definition of the BeanNameAware interface:
----
----
The callback is invoked after population of normal bean properties but before an
The callback is invoked after population of normal bean properties but before an
initialization callback such as `InitializingBean`, `afterPropertiesSet`, or a custom
initialization callback such as `InitializingBean.afterPropertiesSet()` or a custom
init-method.
init-method.
@ -3960,7 +3960,7 @@ dependency type. The following table summarizes the most important `Aware` inter
| `BeanFactoryAware`
| `BeanFactoryAware`
| Declaring `BeanFactory`.
| Declaring `BeanFactory`.
| <<beans-factory-aware >>
| <<beans-bean factory>>
| `BeanNameAware`
| `BeanNameAware`
| Name of the declaring bean.
| Name of the declaring bean.
@ -4142,7 +4142,7 @@ or it may wrap a bean with a proxy. Some Spring AOP infrastructure classes are
implemented as bean post-processors in order to provide proxy-wrapping logic.
implemented as bean post-processors in order to provide proxy-wrapping logic.
An `ApplicationContext` automatically detects any beans that are defined in the
An `ApplicationContext` automatically detects any beans that are defined in the
configuration metadata that implements the `BeanPostProcessor` interface. The
configuration metadata that implement the `BeanPostProcessor` interface. The
`ApplicationContext` registers these beans as post-processors so that they can be called
`ApplicationContext` registers these beans as post-processors so that they can be called
later, upon bean creation. Bean post-processors can be deployed in the container in the
later, upon bean creation. Bean post-processors can be deployed in the container in the
same fashion as any other beans.
same fashion as any other beans.
@ -4673,11 +4673,11 @@ example:
----
----
class SimpleMovieLister {
class SimpleMovieLister {
@Required
@Required
lateinit var movieFinder: MovieFinder
lateinit var movieFinder: MovieFinder
// ...
// ...
}
}
----
----
This annotation indicates that the affected bean property must be populated at
This annotation indicates that the affected bean property must be populated at
@ -5373,7 +5373,7 @@ Letting qualifier values select against target bean names, within the type-match
candidates, does not require a `@Qualifier` annotation at the injection point.
candidates, does not require a `@Qualifier` annotation at the injection point.
If there is no other resolution indicator (such as a qualifier or a primary marker),
If there is no other resolution indicator (such as a qualifier or a primary marker),
for a non-unique dependency situation, Spring matches the injection point name
for a non-unique dependency situation, Spring matches the injection point name
(that is, the field name or parameter name) against the target bean names and choose the
(that is, the field name or parameter name) against the target bean names and chooses the
same-named candidate, if any.
same-named candidate, if any.
====
====
@ -6042,14 +6042,14 @@ example shows:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
.Java
----
----
@Configuration
@Configuration
public class AppConfig {
public class AppConfig {
@Bean
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
return new PropertySourcesPlaceholderConfigurer();
}
}
}
}
----
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
.Kotlin
@ -6075,7 +6075,7 @@ will get properties from `application.properties` and `application.yml` files.
Built-in converter support provided by Spring allows simple type conversion (to `Integer`
Built-in converter support provided by Spring allows simple type conversion (to `Integer`
or `int` for example) to be automatically handled. Multiple comma-separated values can be
or `int` for example) to be automatically handled. Multiple comma-separated values can be
automatically converted to String array without extra effort.
automatically converted to ` String` array without extra effort.
It is possible to provide a default value as following:
It is possible to provide a default value as following:
@ -6099,8 +6099,8 @@ It is possible to provide a default value as following:
class MovieRecommender(@Value("\${catalog.name:defaultCatalog}") private val catalog: String)
class MovieRecommender(@Value("\${catalog.name:defaultCatalog}") private val catalog: String)
----
----
A Spring `BeanPostProcessor` uses a `ConversionService` behind the scene to handle the
A Spring `BeanPostProcessor` uses a `ConversionService` behind the scenes to handle the
process for converting the String value in `@Value` to the target type. If you want to
process for converting the ` String` value in `@Value` to the target type. If you want to
provide conversion support for your own custom type, you can provide your own
provide conversion support for your own custom type, you can provide your own
`ConversionService` bean instance as the following example shows:
`ConversionService` bean instance as the following example shows:
@ -6126,7 +6126,7 @@ provide conversion support for your own custom type, you can provide your own
@Bean
@Bean
fun conversionService(): ConversionService {
fun conversionService(): ConversionService {
return DefaultFormattingConversionService().apply {
return DefaultFormattingConversionService().apply {
addConverter(MyCustomConverter())
addConverter(MyCustomConverter())
}
}
}
}
@ -6315,7 +6315,7 @@ is meta-annotated with `@Component`, as the following example shows:
// ...
// ...
}
}
----
----
<1> The `Component` causes `@Service` to be treated in the same way as `@Component`.
<1> The `@ Component` causes `@Service` to be treated in the same way as `@Component`.
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
.Kotlin
@ -6329,7 +6329,7 @@ is meta-annotated with `@Component`, as the following example shows:
// ...
// ...
}
}
----
----
<1> The `Component` causes `@Service` to be treated in the same way as `@Component`.
<1> The `@ Component` causes `@Service` to be treated in the same way as `@Component`.
You can also combine meta-annotations to create "`composed annotations`". For example,
You can also combine meta-annotations to create "`composed annotations`". For example,
the `@RestController` annotation from Spring MVC is composed of `@Controller` and
the `@RestController` annotation from Spring MVC is composed of `@Controller` and
@ -6591,7 +6591,7 @@ and using "`stub`" repositories instead:
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
excludeFilters = @Filter(Repository.class))
excludeFilters = @Filter(Repository.class))
public class AppConfig {
public class AppConfig {
...
// ...
}
}
----
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
@ -7157,7 +7157,7 @@ configuration, as shown in the following example:
With Gradle 4.6 and later, the dependency should be declared in the `annotationProcessor`
With Gradle 4.6 and later, the dependency should be declared in the `annotationProcessor`
configuration, as shown in the following example:
configuration, as shown in the following example:
[source,groovy,indent=0subs="verbatim,quotes,attributes"]
[source,groovy,indent=0, subs="verbatim,quotes,attributes"]
----
----
dependencies {
dependencies {
annotationProcessor "org.springframework:spring-context-indexer:{spring-version}"
annotationProcessor "org.springframework:spring-context-indexer:{spring-version}"
@ -7631,7 +7631,7 @@ to reduce subtle bugs that can be hard to track down when operating in "`lite`"
****
****
The `@Bean` and `@Configuration` annotations are discussed in depth in the following sections.
The `@Bean` and `@Configuration` annotations are discussed in depth in the following sections.
First, however, we cover the various ways of creating a spring container using by
First, however, we cover the various ways of creating a spring container by using
Java-based configuration.
Java-based configuration.
@ -7758,7 +7758,7 @@ To enable component scanning, you can annotate your `@Configuration` class as fo
@Configuration
@Configuration
@ComponentScan(basePackages = "com.acme") // <1>
@ComponentScan(basePackages = "com.acme") // <1>
public class AppConfig {
public class AppConfig {
...
// ...
}
}
----
----
<1> This annotation enables component scanning.
<1> This annotation enables component scanning.
@ -7892,6 +7892,7 @@ init-param):
`@Bean` is a method-level annotation and a direct analog of the XML `<bean/>` element.
`@Bean` is a method-level annotation and a direct analog of the XML `<bean/>` element.
The annotation supports some of the attributes offered by `<bean/>`, such as:
The annotation supports some of the attributes offered by `<bean/>`, such as:
* <<beans-factory-lifecycle-initializingbean, init-method>>
* <<beans-factory-lifecycle-initializingbean, init-method>>
* <<beans-factory-lifecycle-disposablebean, destroy-method>>
* <<beans-factory-lifecycle-disposablebean, destroy-method>>
* <<beans-factory-autowire,autowiring>>
* <<beans-factory-autowire,autowiring>>
@ -7980,7 +7981,7 @@ return type, as the following example shows:
However, this limits the visibility for advance type prediction to the specified
However, this limits the visibility for advance type prediction to the specified
interface type (`TransferService`). Then, with the full type (`TransferServiceImpl`)
interface type (`TransferService`). Then, with the full type (`TransferServiceImpl`)
known to the container only once, the affected singleton bean has been instantiated.
known to the container only once the affected singleton bean has been instantiated.
Non-lazy singleton beans get instantiated according to their declaration order,
Non-lazy singleton beans get instantiated according to their declaration order,
so you may see different type matching results depending on when another component
so you may see different type matching results depending on when another component
tries to match by a non-declared type (such as `@Autowired TransferServiceImpl`,
tries to match by a non-declared type (such as `@Autowired TransferServiceImpl`,
@ -8295,7 +8296,7 @@ as the following example shows:
@Configuration
@Configuration
public class AppConfig {
public class AppConfig {
@Bean(name = "myThing")
@Bean("myThing")
public Thing thing() {
public Thing thing() {
return new Thing();
return new Thing();
}
}
@ -8388,7 +8389,7 @@ annotation, as the following example shows:
=== Using the `@Configuration` annotation
=== Using the `@Configuration` annotation
`@Configuration` is a class-level annotation indicating that an object is a source of
`@Configuration` is a class-level annotation indicating that an object is a source of
bean definitions. `@Configuration` classes declare beans through `@Bean` annotated
bean definitions. `@Configuration` classes declare beans through `@Bean`- annotated
methods. Calls to `@Bean` methods on `@Configuration` classes can also be used to define
methods. Calls to `@Bean` methods on `@Configuration` classes can also be used to define
inter-bean dependencies. See <<beans-java-basic-concepts>> for a general introduction.
inter-bean dependencies. See <<beans-java-basic-concepts>> for a general introduction.
@ -9137,7 +9138,7 @@ method that returns `true` or `false`. For example, the following listing shows
val attrs = metadata.getAllAnnotationAttributes(Profile::class.java.name)
val attrs = metadata.getAllAnnotationAttributes(Profile::class.java.name)
if (attrs != null) {
if (attrs != null) {
for (value in attrs["value"]!!) {
for (value in attrs["value"]!!) {
if (context.environment.acceptsProfiles(Profiles .of(*value as Array<String>))) {
if (context.environment.acceptsProfiles(Profiles.of(*value as Array<String>))) {
return true
return true
}
}
}
}
@ -9901,7 +9902,7 @@ as a way to provide a default definition for one or more beans. If any
profile is enabled, the default profile does not apply.
profile is enabled, the default profile does not apply.
You can change the name of the default profile by using `setDefaultProfiles()` on
You can change the name of the default profile by using `setDefaultProfiles()` on
the `Environment` or ,declaratively, by using the `spring.profiles.default` property.
the `Environment` or, declaratively, by using the `spring.profiles.default` property.
@ -10720,7 +10721,7 @@ following example shows how to do so:
----
----
It is also possible to add additional runtime filtering by using the `condition` attribute
It is also possible to add additional runtime filtering by using the `condition` attribute
of the annotation that defines a <<expressions, `SpEL` expression>> , which should match
of the annotation that defines a <<expressions, `SpEL` expression>>, which should match
to actually invoke the method for a particular event.
to actually invoke the method for a particular event.
The following example shows how our notifier can be rewritten to be invoked only if the
The following example shows how our notifier can be rewritten to be invoked only if the
@ -10832,10 +10833,12 @@ The following example shows how to do so:
Be aware of the following limitations when using asynchronous events:
Be aware of the following limitations when using asynchronous events:
* If an asynchronous event listener throws an `Exception`, it is not propagated to the
* If an asynchronous event listener throws an `Exception`, it is not propagated to the
caller. See `AsyncUncaughtExceptionHandler` for more details.
caller. See
{api-spring-framework}/aop/interceptor/AsyncUncaughtExceptionHandler.html[`AsyncUncaughtExceptionHandler`]
for more details.
* Asynchronous event listener methods cannot publish a subsequent event by returning a
* Asynchronous event listener methods cannot publish a subsequent event by returning a
value. If you need to publish another event as the result of the processing, inject an
value. If you need to publish another event as the result of the processing, inject an
{api-spring-framework}/aop/interceptor/AsyncUncaughtExceptionHandl er.html[`ApplicationEventPublisher`]
{api-spring-framework}/context/ApplicationEventPublish er.html[`ApplicationEventPublisher`]
to publish the event manually.
to publish the event manually.
@ -11093,8 +11096,8 @@ For a simple deployment of a Spring ApplicationContext as a Java EE RAR file:
. Package
. Package
all application classes into a RAR file (which is a standard JAR file with a different
all application classes into a RAR file (which is a standard JAR file with a different
file extension).
file extension).
.Add all required library JARs into the root of the RAR archive.
. Add all required library JARs into the root of the RAR archive.
.Add a
. Add a
`META-INF/ra.xml` deployment descriptor (as shown in the {api-spring-framework}/jca/context/SpringContextResourceAdapter.html[javadoc for `SpringContextResourceAdapter`])
`META-INF/ra.xml` deployment descriptor (as shown in the {api-spring-framework}/jca/context/SpringContextResourceAdapter.html[javadoc for `SpringContextResourceAdapter`])
and the corresponding Spring XML bean definition file(s) (typically
and the corresponding Spring XML bean definition file(s) (typically
`META-INF/applicationContext.xml`).
`META-INF/applicationContext.xml`).