@@ -474,12 +474,37 @@ bean expose the `Resource` properties it needs, and expect them to be injected i
474
474
475
475
What makes it trivial to then inject these properties is that all application contexts
476
476
register and use a special JavaBeans `PropertyEditor`, which can convert `String` paths
477
- to `Resource` objects. So, if `myBean` has a template property of type `Resource`, it can
478
- be configured with a simple string for that resource, as the following example shows:
477
+ to `Resource` objects. For example, the following `MyBean` class has a `template`
478
+ property of type `Resource`.
479
+
480
+ [source,java,indent=0,subs="verbatim,quotes",role="primary"]
481
+ .Java
482
+ ----
483
+ package example;
484
+
485
+ public class MyBean {
486
+
487
+ private Resource template;
488
+
489
+ public setTemplate(Resource template) {
490
+ this.template = template;
491
+ }
492
+
493
+ // ...
494
+ }
495
+ ----
496
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
497
+ .Kotlin
498
+ ----
499
+ class MyBean(var template: Resource)
500
+ ----
501
+
502
+ In an XML configuration file, the `template` property can be configured with a simple
503
+ string for that resource, as the following example shows:
479
504
480
505
[source,xml,indent=0,subs="verbatim,quotes"]
481
506
----
482
- <bean id="myBean" class="... ">
507
+ <bean id="myBean" class="example.MyBean ">
483
508
<property name="template" value="some/resource/path/myTemplate.txt"/>
484
509
</bean>
485
510
----
@@ -503,6 +528,66 @@ latter being used to access a file in the filesystem):
503
528
<property name="template" value="file:///some/resource/path/myTemplate.txt"/>
504
529
----
505
530
531
+ If the `MyBean` class is refactored for use with annotation-driven configuration, the
532
+ path to `myTemplate.txt` can be stored under a key named `template.path` -- for example,
533
+ in a properties file made available to the Spring `Environment` (see
534
+ <<beans-environment>>). The template path can then be referenced via the `@Value`
535
+ annotation using a property placeholder (see <<beans-value-annotations>>). Spring will
536
+ retrieve the value of the template path as a string, and a special `PropertyEditor` will
537
+ convert the string to a `Resource` object to be injected into the `MyBean` constructor.
538
+ The following example demonstrates how to achieve this.
539
+
540
+ [source,java,indent=0,subs="verbatim,quotes",role="primary"]
541
+ .Java
542
+ ----
543
+ @Component
544
+ public class MyBean {
545
+
546
+ private final Resource template;
547
+
548
+ public MyBean(@Value("${template.path}") Resource template) {
549
+ this.template = template;
550
+ }
551
+
552
+ // ...
553
+ }
554
+ ----
555
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
556
+ .Kotlin
557
+ ----
558
+ @Component
559
+ class MyBean(@Value("\${template.path}") private val template: Resource)
560
+ ----
561
+
562
+ If we want to support multiple templates discovered under the same path in multiple
563
+ locations in the classpath -- for example, in multiple jars in the classpath -- we can
564
+ use the special `classpath*:` prefix and wildcarding to define a `templates.path` key as
565
+ `classpath*:/config/templates/*.txt`. If we redefine the `MyBean` class as follows,
566
+ Spring will convert the template path pattern into an array of `Resource` objects that
567
+ can be injected into the `MyBean` constructor.
568
+
569
+ [source,java,indent=0,subs="verbatim,quotes",role="primary"]
570
+ .Java
571
+ ----
572
+ @Component
573
+ public class MyBean {
574
+
575
+ private final Resource[] templates;
576
+
577
+ public MyBean(@Value("${templates.path}") Resource[] templates) {
578
+ this.templates = templates;
579
+ }
580
+
581
+ // ...
582
+ }
583
+ ----
584
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
585
+ .Kotlin
586
+ ----
587
+ @Component
588
+ class MyBean(@Value("\${templates.path}") private val templates: Resource[])
589
+ ----
590
+
506
591
507
592
508
593
0 commit comments