@@ -5200,6 +5200,7 @@ with specific arguments, narrowing the set of type matches so that a specific be
5200
5200
chosen for each argument. In the simplest case, this can be a plain descriptive value, as
5201
5201
shown in the following example:
5202
5202
5203
+ --
5203
5204
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5204
5205
.Java
5205
5206
----
@@ -5224,10 +5225,12 @@ shown in the following example:
5224
5225
// ...
5225
5226
}
5226
5227
----
5228
+ --
5227
5229
5228
5230
You can also specify the `@Qualifier` annotation on individual constructor arguments or
5229
5231
method parameters, as shown in the following example:
5230
5232
5233
+ --
5231
5234
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5232
5235
.Java
5233
5236
----
@@ -5266,9 +5269,11 @@ method parameters, as shown in the following example:
5266
5269
// ...
5267
5270
}
5268
5271
----
5272
+ --
5269
5273
5270
5274
The following example shows corresponding bean definitions.
5271
5275
5276
+ --
5272
5277
[source,xml,indent=0,subs="verbatim,quotes"]
5273
5278
----
5274
5279
<?xml version="1.0" encoding="UTF-8"?>
@@ -5302,6 +5307,7 @@ The following example shows corresponding bean definitions.
5302
5307
is qualified with the same value.
5303
5308
<2> The bean with the `action` qualifier value is wired with the constructor argument that
5304
5309
is qualified with the same value.
5310
+ --
5305
5311
5306
5312
For a fallback match, the bean name is considered a default qualifier value. Thus, you
5307
5313
can define the bean with an `id` of `main` instead of the nested qualifier element, leading
@@ -5379,6 +5385,7 @@ constructor or a multi-argument method.
5379
5385
You can create your own custom qualifier annotations. To do so, define an annotation and
5380
5386
provide the `@Qualifier` annotation within your definition, as the following example shows:
5381
5387
5388
+ --
5382
5389
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5383
5390
.Java
5384
5391
----
@@ -5398,10 +5405,12 @@ provide the `@Qualifier` annotation within your definition, as the following exa
5398
5405
@Qualifier
5399
5406
annotation class Genre(val value: String)
5400
5407
----
5408
+ --
5401
5409
5402
5410
Then you can provide the custom qualifier on autowired fields and parameters, as the
5403
5411
following example shows:
5404
5412
5413
+ --
5405
5414
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5406
5415
.Java
5407
5416
----
@@ -5440,6 +5449,7 @@ following example shows:
5440
5449
// ...
5441
5450
}
5442
5451
----
5452
+ --
5443
5453
5444
5454
Next, you can provide the information for the candidate bean definitions. You can add
5445
5455
`<qualifier/>` tags as sub-elements of the `<bean/>` tag and then specify the `type` and
@@ -5448,6 +5458,7 @@ fully-qualified class name of the annotation. Alternately, as a convenience if n
5448
5458
conflicting names exists, you can use the short class name. The following example
5449
5459
demonstrates both approaches:
5450
5460
5461
+ --
5451
5462
[source,xml,indent=0,subs="verbatim,quotes"]
5452
5463
----
5453
5464
<?xml version="1.0" encoding="UTF-8"?>
@@ -5475,6 +5486,7 @@ demonstrates both approaches:
5475
5486
5476
5487
</beans>
5477
5488
----
5489
+ --
5478
5490
5479
5491
In <<beans-classpath-scanning>>, you can see an annotation-based alternative to
5480
5492
providing the qualifier metadata in XML. Specifically, see <<beans-scanning-qualifiers>>.
@@ -5485,6 +5497,7 @@ several different types of dependencies. For example, you may provide an offline
5485
5497
catalog that can be searched when no Internet connection is available. First, define
5486
5498
the simple annotation, as the following example shows:
5487
5499
5500
+ --
5488
5501
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5489
5502
.Java
5490
5503
----
@@ -5502,10 +5515,12 @@ the simple annotation, as the following example shows:
5502
5515
@Qualifier
5503
5516
annotation class Offline
5504
5517
----
5518
+ --
5505
5519
5506
5520
Then add the annotation to the field or property to be autowired, as shown in the
5507
5521
following example:
5508
5522
5523
+ --
5509
5524
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5510
5525
.Java
5511
5526
----
@@ -5533,9 +5548,11 @@ class MovieRecommender {
5533
5548
}
5534
5549
----
5535
5550
<1> This line adds the `@Offline` annotation.
5551
+ --
5536
5552
5537
5553
Now the bean definition only needs a qualifier `type`, as shown in the following example:
5538
5554
5555
+ --
5539
5556
[source,xml,indent=0,subs="verbatim,quotes"]
5540
5557
----
5541
5558
<bean class="example.SimpleMovieCatalog">
@@ -5544,6 +5561,7 @@ Now the bean definition only needs a qualifier `type`, as shown in the following
5544
5561
</bean>
5545
5562
----
5546
5563
<1> This element specifies the qualifier.
5564
+ --
5547
5565
5548
5566
5549
5567
You can also define custom qualifier annotations that accept named attributes in
@@ -5552,6 +5570,7 @@ then specified on a field or parameter to be autowired, a bean definition must m
5552
5570
all such attribute values to be considered an autowire candidate. As an example,
5553
5571
consider the following annotation definition:
5554
5572
5573
+ --
5555
5574
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5556
5575
.Java
5557
5576
----
@@ -5573,9 +5592,11 @@ consider the following annotation definition:
5573
5592
@Qualifier
5574
5593
annotation class MovieQualifier(val genre: String, val format: Format)
5575
5594
----
5595
+ --
5576
5596
5577
5597
In this case `Format` is an enum, defined as follows:
5578
5598
5599
+ --
5579
5600
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5580
5601
.Java
5581
5602
----
@@ -5590,10 +5611,12 @@ In this case `Format` is an enum, defined as follows:
5590
5611
VHS, DVD, BLURAY
5591
5612
}
5592
5613
----
5614
+ --
5593
5615
5594
5616
The fields to be autowired are annotated with the custom qualifier and include values
5595
5617
for both attributes: `genre` and `format`, as the following example shows:
5596
5618
5619
+ --
5597
5620
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5598
5621
.Java
5599
5622
----
@@ -5642,6 +5665,7 @@ for both attributes: `genre` and `format`, as the following example shows:
5642
5665
// ...
5643
5666
}
5644
5667
----
5668
+ --
5645
5669
5646
5670
Finally, the bean definitions should contain matching qualifier values. This example
5647
5671
also demonstrates that you can use bean meta attributes instead of the
@@ -5650,6 +5674,7 @@ precedence, but the autowiring mechanism falls back on the values provided withi
5650
5674
`<meta/>` tags if no such qualifier is present, as in the last two bean definitions in
5651
5675
the following example:
5652
5676
5677
+ --
5653
5678
[source,xml,indent=0,subs="verbatim,quotes"]
5654
5679
----
5655
5680
<?xml version="1.0" encoding="UTF-8"?>
@@ -5693,6 +5718,7 @@ the following example:
5693
5718
5694
5719
</beans>
5695
5720
----
5721
+ --
5696
5722
5697
5723
5698
5724
@@ -5824,6 +5850,7 @@ endpoints. Spring supports this pattern for Spring-managed objects as well.
5824
5850
the bean name to be injected. In other words, it follows by-name semantics,
5825
5851
as demonstrated in the following example:
5826
5852
5853
+ --
5827
5854
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5828
5855
.Java
5829
5856
----
@@ -5849,13 +5876,15 @@ class SimpleMovieLister {
5849
5876
}
5850
5877
----
5851
5878
<1> This line injects a `@Resource`.
5879
+ --
5852
5880
5853
5881
5854
5882
If no name is explicitly specified, the default name is derived from the field name or
5855
5883
setter method. In case of a field, it takes the field name. In case of a setter method,
5856
5884
it takes the bean property name. The following example is going to have the bean
5857
5885
named `movieFinder` injected into its setter method:
5858
5886
5887
+ --
5859
5888
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5860
5889
.Java
5861
5890
----
@@ -5879,6 +5908,7 @@ named `movieFinder` injected into its setter method:
5879
5908
5880
5909
}
5881
5910
----
5911
+ --
5882
5912
5883
5913
NOTE: The name provided with the annotation is resolved as a bean name by the
5884
5914
`ApplicationContext` of which the `CommonAnnotationBeanPostProcessor` is aware.
@@ -5897,6 +5927,7 @@ Thus, in the following example, the `customerPreferenceDao` field first looks fo
5897
5927
named "customerPreferenceDao" and then falls back to a primary type match for the type
5898
5928
`CustomerPreferenceDao`:
5899
5929
5930
+ --
5900
5931
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
5901
5932
.Java
5902
5933
----
@@ -5934,6 +5965,7 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
5934
5965
----
5935
5966
<1> The `context` field is injected based on the known resolvable dependency type:
5936
5967
`ApplicationContext`.
5968
+ --
5937
5969
5938
5970
[[beans-value-annotations]]
5939
5971
=== Using `@Value`
@@ -9489,6 +9521,7 @@ annotation lets you indicate that a component is eligible for registration
9489
9521
when one or more specified profiles are active. Using our preceding example, we
9490
9522
can rewrite the `dataSource` configuration as follows:
9491
9523
9524
+ --
9492
9525
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
9493
9526
.Java
9494
9527
----
@@ -9523,7 +9556,9 @@ can rewrite the `dataSource` configuration as follows:
9523
9556
}
9524
9557
}
9525
9558
----
9559
+ --
9526
9560
9561
+ --
9527
9562
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
9528
9563
.Java
9529
9564
----
@@ -9555,6 +9590,7 @@ can rewrite the `dataSource` configuration as follows:
9555
9590
}
9556
9591
----
9557
9592
<1> `@Bean(destroyMethod = "")` disables default destroy method inference.
9593
+ --
9558
9594
9559
9595
NOTE: As mentioned earlier, with `@Bean` methods, you typically choose to use programmatic
9560
9596
JNDI lookups, by using either Spring's `JndiTemplate`/`JndiLocatorDelegate` helpers or the
@@ -9579,6 +9615,7 @@ of creating a custom composed annotation. The following example defines a custom
9579
9615
`@Production` annotation that you can use as a drop-in replacement for
9580
9616
`@Profile("production")`:
9581
9617
9618
+ --
9582
9619
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
9583
9620
.Java
9584
9621
----
@@ -9596,6 +9633,7 @@ of creating a custom composed annotation. The following example defines a custom
9596
9633
@Profile("production")
9597
9634
annotation class Production
9598
9635
----
9636
+ --
9599
9637
9600
9638
TIP: If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
9601
9639
`@Import` annotations associated with that class are bypassed unless one or more of
@@ -9610,6 +9648,7 @@ active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if
9610
9648
of a configuration class (for example, for alternative variants of a particular bean), as
9611
9649
the following example shows:
9612
9650
9651
+ --
9613
9652
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
9614
9653
.Java
9615
9654
----
@@ -9661,6 +9700,7 @@ the following example shows:
9661
9700
----
9662
9701
<1> The `standaloneDataSource` method is available only in the `development` profile.
9663
9702
<2> The `jndiDataSource` method is available only in the `production` profile.
9703
+ --
9664
9704
9665
9705
[NOTE]
9666
9706
====
0 commit comments