Skip to content

Commit a2b7b1c

Browse files
committed
Merge branch '6.1.x'
2 parents 9b28e58 + 61b5b1e commit a2b7b1c

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc

+42-21
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ Java::
578578
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
579579
if (attrs != null) {
580580
for (Object value : attrs.get("value")) {
581-
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
581+
if (context.getEnvironment().matchesProfiles((String[]) value)) {
582582
return true;
583583
}
584584
}
@@ -597,7 +597,7 @@ Kotlin::
597597
val attrs = metadata.getAllAnnotationAttributes(Profile::class.java.name)
598598
if (attrs != null) {
599599
for (value in attrs["value"]!!) {
600-
if (context.environment.acceptsProfiles(Profiles.of(*value as Array<String>))) {
600+
if (context.environment.matchesProfiles(*value as Array<String>)) {
601601
return true
602602
}
603603
}
@@ -636,14 +636,14 @@ options for using `@Configuration` classes in this kind of "`XML-centric`" situa
636636
[[beans-java-combining-xml-centric-declare-as-bean]]
637637
==== Declaring `@Configuration` classes as plain Spring `<bean/>` elements
638638

639-
Remember that `@Configuration` classes are ultimately bean definitions in the
640-
container. In this series examples, we create a `@Configuration` class named `AppConfig` and
639+
Remember that `@Configuration` classes are ultimately bean definitions in the container.
640+
In this series of examples, we create a `@Configuration` class named `AppConfig` and
641641
include it within `system-test-config.xml` as a `<bean/>` definition. Because
642642
`<context:annotation-config/>` is switched on, the container recognizes the
643643
`@Configuration` annotation and processes the `@Bean` methods declared in `AppConfig`
644644
properly.
645645

646-
The following example shows an ordinary configuration class in Java:
646+
The following example shows the `AppConfig` configuration class in Java and Kotlin:
647647

648648
[tabs]
649649
======
@@ -664,7 +664,7 @@ Java::
664664
665665
@Bean
666666
public TransferService transferService() {
667-
return new TransferService(accountRepository());
667+
return new TransferServiceImpl(accountRepository());
668668
}
669669
}
670670
----
@@ -697,6 +697,7 @@ The following example shows part of a sample `system-test-config.xml` file:
697697
<beans>
698698
<!-- enable processing of annotations such as @Autowired and @Configuration -->
699699
<context:annotation-config/>
700+
700701
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
701702
702703
<bean class="com.acme.AppConfig"/>
@@ -743,8 +744,8 @@ Kotlin::
743744
----
744745
======
745746

746-
NOTE: In `system-test-config.xml` file, the `AppConfig` `<bean/>` does not declare an `id`
747-
element. While it would be acceptable to do so, it is unnecessary, given that no other bean
747+
NOTE: In the `system-test-config.xml` file, the `AppConfig` `<bean/>` does not declare an `id`
748+
attribute. While it would be acceptable to do so, it is unnecessary, given that no other bean
748749
ever refers to it, and it is unlikely to be explicitly fetched from the container by name.
749750
Similarly, the `DataSource` bean is only ever autowired by type, so an explicit bean `id`
750751
is not strictly required.
@@ -755,8 +756,8 @@ is not strictly required.
755756

756757
Because `@Configuration` is meta-annotated with `@Component`, `@Configuration`-annotated
757758
classes are automatically candidates for component scanning. Using the same scenario as
758-
described in the previous example, we can redefine `system-test-config.xml` to take advantage of component-scanning.
759-
Note that, in this case, we need not explicitly declare
759+
described in the previous example, we can redefine `system-test-config.xml` to take
760+
advantage of component-scanning. Note that, in this case, we need not explicitly declare
760761
`<context:annotation-config/>`, because `<context:component-scan/>` enables the same
761762
functionality.
762763

@@ -767,6 +768,7 @@ The following example shows the modified `system-test-config.xml` file:
767768
<beans>
768769
<!-- picks up and registers AppConfig as a bean definition -->
769770
<context:component-scan base-package="com.acme"/>
771+
770772
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
771773
772774
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
@@ -781,13 +783,12 @@ The following example shows the modified `system-test-config.xml` file:
781783
=== `@Configuration` Class-centric Use of XML with `@ImportResource`
782784

783785
In applications where `@Configuration` classes are the primary mechanism for configuring
784-
the container, it is still likely necessary to use at least some XML. In these
785-
scenarios, you can use `@ImportResource` and define only as much XML as you need. Doing
786-
so achieves a "`Java-centric`" approach to configuring the container and keeps XML to a
787-
bare minimum. The following example (which includes a configuration class, an XML file
788-
that defines a bean, a properties file, and the `main` class) shows how to use
789-
the `@ImportResource` annotation to achieve "`Java-centric`" configuration that uses XML
790-
as needed:
786+
the container, it may still be necessary to use at least some XML. In such scenarios, you
787+
can use `@ImportResource` and define only as much XML as you need. Doing so achieves a
788+
"`Java-centric`" approach to configuring the container and keeps XML to a bare minimum.
789+
The following example (which includes a configuration class, an XML file that defines a
790+
bean, a properties file, and the `main()` method) shows how to use the `@ImportResource`
791+
annotation to achieve "`Java-centric`" configuration that uses XML as needed:
791792

792793
[tabs]
793794
======
@@ -812,6 +813,17 @@ Java::
812813
public DataSource dataSource() {
813814
return new DriverManagerDataSource(url, username, password);
814815
}
816+
817+
@Bean
818+
public AccountRepository accountRepository(DataSource dataSource) {
819+
return new JdbcAccountRepository(dataSource);
820+
}
821+
822+
@Bean
823+
public TransferService transferService(AccountRepository accountRepository) {
824+
return new TransferServiceImpl(accountRepository);
825+
}
826+
815827
}
816828
----
817829
@@ -836,21 +848,32 @@ Kotlin::
836848
fun dataSource(): DataSource {
837849
return DriverManagerDataSource(url, username, password)
838850
}
851+
852+
@Bean
853+
fun accountRepository(dataSource: DataSource): AccountRepository {
854+
return JdbcAccountRepository(dataSource)
855+
}
856+
857+
@Bean
858+
fun transferService(accountRepository: AccountRepository): TransferService {
859+
return TransferServiceImpl(accountRepository)
860+
}
861+
839862
}
840863
----
841864
======
842865

866+
.properties-config.xml
843867
[source,xml,indent=0,subs="verbatim,quotes"]
844868
----
845-
properties-config.xml
846869
<beans>
847870
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
848871
</beans>
849872
----
850873

874+
.jdbc.properties
851875
[literal,subs="verbatim,quotes"]
852876
----
853-
jdbc.properties
854877
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
855878
jdbc.username=sa
856879
jdbc.password=
@@ -883,5 +906,3 @@ Kotlin::
883906
----
884907
======
885908

886-
887-

0 commit comments

Comments
 (0)