@@ -599,14 +599,14 @@ options for using `@Configuration` classes in this kind of "`XML-centric`" situa
599
599
[[beans-java-combining-xml-centric-declare-as-bean]]
600
600
==== Declaring `@Configuration` classes as plain Spring `<bean/>` elements
601
601
602
- Remember that `@Configuration` classes are ultimately bean definitions in the
603
- container. In this series examples, we create a `@Configuration` class named `AppConfig` and
602
+ Remember that `@Configuration` classes are ultimately bean definitions in the container.
603
+ In this series of examples, we create a `@Configuration` class named `AppConfig` and
604
604
include it within `system-test-config.xml` as a `<bean/>` definition. Because
605
605
`<context:annotation-config/>` is switched on, the container recognizes the
606
606
`@Configuration` annotation and processes the `@Bean` methods declared in `AppConfig`
607
607
properly.
608
608
609
- The following example shows an ordinary configuration class in Java:
609
+ The following example shows the `AppConfig` configuration class in Java and Kotlin :
610
610
611
611
[tabs]
612
612
======
@@ -660,6 +660,7 @@ The following example shows part of a sample `system-test-config.xml` file:
660
660
<beans>
661
661
<!-- enable processing of annotations such as @Autowired and @Configuration -->
662
662
<context:annotation-config/>
663
+
663
664
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
664
665
665
666
<bean class="com.acme.AppConfig"/>
@@ -706,8 +707,8 @@ Kotlin::
706
707
----
707
708
======
708
709
709
- NOTE: In `system-test-config.xml` file, the `AppConfig` `<bean/>` does not declare an `id`
710
- element . While it would be acceptable to do so, it is unnecessary, given that no other bean
710
+ NOTE: In the `system-test-config.xml` file, the `AppConfig` `<bean/>` does not declare an `id`
711
+ attribute . While it would be acceptable to do so, it is unnecessary, given that no other bean
711
712
ever refers to it, and it is unlikely to be explicitly fetched from the container by name.
712
713
Similarly, the `DataSource` bean is only ever autowired by type, so an explicit bean `id`
713
714
is not strictly required.
@@ -718,8 +719,8 @@ is not strictly required.
718
719
719
720
Because `@Configuration` is meta-annotated with `@Component`, `@Configuration`-annotated
720
721
classes are automatically candidates for component scanning. Using the same scenario as
721
- described in the previous example, we can redefine `system-test-config.xml` to take advantage of component-scanning.
722
- Note that, in this case, we need not explicitly declare
722
+ described in the previous example, we can redefine `system-test-config.xml` to take
723
+ advantage of component-scanning. Note that, in this case, we need not explicitly declare
723
724
`<context:annotation-config/>`, because `<context:component-scan/>` enables the same
724
725
functionality.
725
726
@@ -730,6 +731,7 @@ The following example shows the modified `system-test-config.xml` file:
730
731
<beans>
731
732
<!-- picks up and registers AppConfig as a bean definition -->
732
733
<context:component-scan base-package="com.acme"/>
734
+
733
735
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
734
736
735
737
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
@@ -744,13 +746,12 @@ The following example shows the modified `system-test-config.xml` file:
744
746
=== `@Configuration` Class-centric Use of XML with `@ImportResource`
745
747
746
748
In applications where `@Configuration` classes are the primary mechanism for configuring
747
- the container, it is still likely necessary to use at least some XML. In these
748
- scenarios, you can use `@ImportResource` and define only as much XML as you need. Doing
749
- so achieves a "`Java-centric`" approach to configuring the container and keeps XML to a
750
- bare minimum. The following example (which includes a configuration class, an XML file
751
- that defines a bean, a properties file, and the `main` class) shows how to use
752
- the `@ImportResource` annotation to achieve "`Java-centric`" configuration that uses XML
753
- as needed:
749
+ the container, it may still be necessary to use at least some XML. In such scenarios, you
750
+ can use `@ImportResource` and define only as much XML as you need. Doing so achieves a
751
+ "`Java-centric`" approach to configuring the container and keeps XML to a bare minimum.
752
+ The following example (which includes a configuration class, an XML file that defines a
753
+ bean, a properties file, and the `main()` method) shows how to use the `@ImportResource`
754
+ annotation to achieve "`Java-centric`" configuration that uses XML as needed:
754
755
755
756
[tabs]
756
757
======
@@ -803,17 +804,17 @@ Kotlin::
803
804
----
804
805
======
805
806
807
+ .properties-config.xml
806
808
[source,xml,indent=0,subs="verbatim,quotes"]
807
809
----
808
- properties-config.xml
809
810
<beans>
810
811
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
811
812
</beans>
812
813
----
813
814
815
+ .jdbc.properties
814
816
[literal,subs="verbatim,quotes"]
815
817
----
816
- jdbc.properties
817
818
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
818
819
jdbc.username=sa
819
820
jdbc.password=
@@ -846,5 +847,3 @@ Kotlin::
846
847
----
847
848
======
848
849
849
-
850
-
0 commit comments