Skip to content

Commit

Permalink
Ensure code listing callouts are displayed incorrectly in core-beans.…
Browse files Browse the repository at this point in the history
…adoc

Closes gh-29457
  • Loading branch information
sbrannen committed Nov 20, 2022
1 parent 9378493 commit 32a5830
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions framework-docs/src/docs/asciidoc/core/core-beans.adoc
Expand Up @@ -5200,6 +5200,7 @@ with specific arguments, narrowing the set of type matches so that a specific be
chosen for each argument. In the simplest case, this can be a plain descriptive value, as
shown in the following example:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5224,10 +5225,12 @@ shown in the following example:
// ...
}
----
--

You can also specify the `@Qualifier` annotation on individual constructor arguments or
method parameters, as shown in the following example:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -5266,9 +5269,11 @@ method parameters, as shown in the following example:
// ...
}
----
--

The following example shows corresponding bean definitions.

--
[source,xml,indent=0,subs="verbatim,quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -5302,6 +5307,7 @@ The following example shows corresponding bean definitions.
is qualified with the same value.
<2> The bean with the `action` qualifier value is wired with the constructor argument that
is qualified with the same value.
--

For a fallback match, the bean name is considered a default qualifier value. Thus, you
can define the bean with an `id` of `main` instead of the nested qualifier element, leading
Expand Down Expand Up @@ -5379,6 +5385,7 @@ constructor or a multi-argument method.
You can create your own custom qualifier annotations. To do so, define an annotation and
provide the `@Qualifier` annotation within your definition, as the following example shows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5398,10 +5405,12 @@ provide the `@Qualifier` annotation within your definition, as the following exa
@Qualifier
annotation class Genre(val value: String)
----
--

Then you can provide the custom qualifier on autowired fields and parameters, as the
following example shows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -5440,6 +5449,7 @@ following example shows:
// ...
}
----
--

Next, you can provide the information for the candidate bean definitions. You can add
`<qualifier/>` tags as sub-elements of the `<bean/>` tag and then specify the `type` and
Expand All @@ -5448,6 +5458,7 @@ fully-qualified class name of the annotation. Alternately, as a convenience if n
conflicting names exists, you can use the short class name. The following example
demonstrates both approaches:

--
[source,xml,indent=0,subs="verbatim,quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -5475,6 +5486,7 @@ demonstrates both approaches:
</beans>
----
--

In <<beans-classpath-scanning>>, you can see an annotation-based alternative to
providing the qualifier metadata in XML. Specifically, see <<beans-scanning-qualifiers>>.
Expand All @@ -5485,6 +5497,7 @@ several different types of dependencies. For example, you may provide an offline
catalog that can be searched when no Internet connection is available. First, define
the simple annotation, as the following example shows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5502,10 +5515,12 @@ the simple annotation, as the following example shows:
@Qualifier
annotation class Offline
----
--

Then add the annotation to the field or property to be autowired, as shown in the
following example:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -5533,9 +5548,11 @@ class MovieRecommender {
}
----
<1> This line adds the `@Offline` annotation.
--

Now the bean definition only needs a qualifier `type`, as shown in the following example:

--
[source,xml,indent=0,subs="verbatim,quotes"]
----
<bean class="example.SimpleMovieCatalog">
Expand All @@ -5544,6 +5561,7 @@ Now the bean definition only needs a qualifier `type`, as shown in the following
</bean>
----
<1> This element specifies the qualifier.
--


You can also define custom qualifier annotations that accept named attributes in
Expand All @@ -5552,6 +5570,7 @@ then specified on a field or parameter to be autowired, a bean definition must m
all such attribute values to be considered an autowire candidate. As an example,
consider the following annotation definition:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5573,9 +5592,11 @@ consider the following annotation definition:
@Qualifier
annotation class MovieQualifier(val genre: String, val format: Format)
----
--

In this case `Format` is an enum, defined as follows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5590,10 +5611,12 @@ In this case `Format` is an enum, defined as follows:
VHS, DVD, BLURAY
}
----
--

The fields to be autowired are annotated with the custom qualifier and include values
for both attributes: `genre` and `format`, as the following example shows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -5642,6 +5665,7 @@ for both attributes: `genre` and `format`, as the following example shows:
// ...
}
----
--

Finally, the bean definitions should contain matching qualifier values. This example
also demonstrates that you can use bean meta attributes instead of the
Expand All @@ -5650,6 +5674,7 @@ precedence, but the autowiring mechanism falls back on the values provided withi
`<meta/>` tags if no such qualifier is present, as in the last two bean definitions in
the following example:

--
[source,xml,indent=0,subs="verbatim,quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -5693,6 +5718,7 @@ the following example:
</beans>
----
--



Expand Down Expand Up @@ -5824,6 +5850,7 @@ endpoints. Spring supports this pattern for Spring-managed objects as well.
the bean name to be injected. In other words, it follows by-name semantics,
as demonstrated in the following example:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5849,13 +5876,15 @@ class SimpleMovieLister {
}
----
<1> This line injects a `@Resource`.
--


If no name is explicitly specified, the default name is derived from the field name or
setter method. In case of a field, it takes the field name. In case of a setter method,
it takes the bean property name. The following example is going to have the bean
named `movieFinder` injected into its setter method:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -5879,6 +5908,7 @@ named `movieFinder` injected into its setter method:
}
----
--

NOTE: The name provided with the annotation is resolved as a bean name by the
`ApplicationContext` of which the `CommonAnnotationBeanPostProcessor` is aware.
Expand All @@ -5897,6 +5927,7 @@ Thus, in the following example, the `customerPreferenceDao` field first looks fo
named "customerPreferenceDao" and then falls back to a primary type match for the type
`CustomerPreferenceDao`:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -5934,6 +5965,7 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
----
<1> The `context` field is injected based on the known resolvable dependency type:
`ApplicationContext`.
--

[[beans-value-annotations]]
=== Using `@Value`
Expand Down Expand Up @@ -9489,6 +9521,7 @@ annotation lets you indicate that a component is eligible for registration
when one or more specified profiles are active. Using our preceding example, we
can rewrite the `dataSource` configuration as follows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -9523,7 +9556,9 @@ can rewrite the `dataSource` configuration as follows:
}
}
----
--

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -9555,6 +9590,7 @@ can rewrite the `dataSource` configuration as follows:
}
----
<1> `@Bean(destroyMethod = "")` disables default destroy method inference.
--

NOTE: As mentioned earlier, with `@Bean` methods, you typically choose to use programmatic
JNDI lookups, by using either Spring's `JndiTemplate`/`JndiLocatorDelegate` helpers or the
Expand All @@ -9579,6 +9615,7 @@ of creating a custom composed annotation. The following example defines a custom
`@Production` annotation that you can use as a drop-in replacement for
`@Profile("production")`:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand All @@ -9596,6 +9633,7 @@ of creating a custom composed annotation. The following example defines a custom
@Profile("production")
annotation class Production
----
--

TIP: If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
`@Import` annotations associated with that class are bypassed unless one or more of
Expand All @@ -9610,6 +9648,7 @@ active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if
of a configuration class (for example, for alternative variants of a particular bean), as
the following example shows:

--
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
Expand Down Expand Up @@ -9661,6 +9700,7 @@ the following example shows:
----
<1> The `standaloneDataSource` method is available only in the `development` profile.
<2> The `jndiDataSource` method is available only in the `production` profile.
--

[NOTE]
====
Expand Down

0 comments on commit 32a5830

Please sign in to comment.