From 4fa27197a7cb5a6aa31ba642c038c4a6f6a473aa Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:06:52 +0200 Subject: [PATCH 1/3] Fix titles for code listings and improve wording --- .../java/composing-configuration-classes.adoc | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc index dab7103ab3..b45ac0de66 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc @@ -599,14 +599,14 @@ options for using `@Configuration` classes in this kind of "`XML-centric`" situa [[beans-java-combining-xml-centric-declare-as-bean]] ==== Declaring `@Configuration` classes as plain Spring `` elements -Remember that `@Configuration` classes are ultimately bean definitions in the -container. In this series examples, we create a `@Configuration` class named `AppConfig` and +Remember that `@Configuration` classes are ultimately bean definitions in the container. +In this series of examples, we create a `@Configuration` class named `AppConfig` and include it within `system-test-config.xml` as a `` definition. Because `` is switched on, the container recognizes the `@Configuration` annotation and processes the `@Bean` methods declared in `AppConfig` properly. -The following example shows an ordinary configuration class in Java: +The following example shows the `AppConfig` configuration class in Java and Kotlin: [tabs] ====== @@ -660,6 +660,7 @@ The following example shows part of a sample `system-test-config.xml` file: + @@ -706,8 +707,8 @@ Kotlin:: ---- ====== -NOTE: In `system-test-config.xml` file, the `AppConfig` `` does not declare an `id` -element. While it would be acceptable to do so, it is unnecessary, given that no other bean +NOTE: In the `system-test-config.xml` file, the `AppConfig` `` does not declare an `id` +attribute. While it would be acceptable to do so, it is unnecessary, given that no other bean ever refers to it, and it is unlikely to be explicitly fetched from the container by name. Similarly, the `DataSource` bean is only ever autowired by type, so an explicit bean `id` is not strictly required. @@ -718,8 +719,8 @@ is not strictly required. Because `@Configuration` is meta-annotated with `@Component`, `@Configuration`-annotated classes are automatically candidates for component scanning. Using the same scenario as -described in the previous example, we can redefine `system-test-config.xml` to take advantage of component-scanning. -Note that, in this case, we need not explicitly declare +described in the previous example, we can redefine `system-test-config.xml` to take +advantage of component-scanning. Note that, in this case, we need not explicitly declare ``, because `` enables the same functionality. @@ -730,6 +731,7 @@ The following example shows the modified `system-test-config.xml` file: + @@ -744,13 +746,12 @@ The following example shows the modified `system-test-config.xml` file: === `@Configuration` Class-centric Use of XML with `@ImportResource` In applications where `@Configuration` classes are the primary mechanism for configuring -the container, it is still likely necessary to use at least some XML. In these -scenarios, you can use `@ImportResource` and define only as much XML as you need. Doing -so achieves a "`Java-centric`" approach to configuring the container and keeps XML to a -bare minimum. The following example (which includes a configuration class, an XML file -that defines a bean, a properties file, and the `main` class) shows how to use -the `@ImportResource` annotation to achieve "`Java-centric`" configuration that uses XML -as needed: +the container, it may still be necessary to use at least some XML. In such scenarios, you +can use `@ImportResource` and define only as much XML as you need. Doing so achieves a +"`Java-centric`" approach to configuring the container and keeps XML to a bare minimum. +The following example (which includes a configuration class, an XML file that defines a +bean, a properties file, and the `main()` method) shows how to use the `@ImportResource` +annotation to achieve "`Java-centric`" configuration that uses XML as needed: [tabs] ====== @@ -803,17 +804,17 @@ Kotlin:: ---- ====== +.properties-config.xml [source,xml,indent=0,subs="verbatim,quotes"] ---- - properties-config.xml ---- +.jdbc.properties [literal,subs="verbatim,quotes"] ---- -jdbc.properties jdbc.url=jdbc:hsqldb:hsql://localhost/xdb jdbc.username=sa jdbc.password= @@ -846,5 +847,3 @@ Kotlin:: ---- ====== - - From 22abcf9aefd2effef488e1cbe269a9e0639c505a Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:25:04 +0200 Subject: [PATCH 2/3] Update examples to use Environment#matchesProfiles() --- .../core/beans/java/composing-configuration-classes.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc index b45ac0de66..a30f5c26e2 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc @@ -541,7 +541,7 @@ Java:: MultiValueMap attrs = metadata.getAllAnnotationAttributes(Profile.class.getName()); if (attrs != null) { for (Object value : attrs.get("value")) { - if (context.getEnvironment().acceptsProfiles(((String[]) value))) { + if (context.getEnvironment().matchesProfiles((String[]) value)) { return true; } } @@ -560,7 +560,7 @@ Kotlin:: val attrs = metadata.getAllAnnotationAttributes(Profile::class.java.name) if (attrs != null) { for (value in attrs["value"]!!) { - if (context.environment.acceptsProfiles(Profiles.of(*value as Array))) { + if (context.environment.matchesProfiles(*value as Array)) { return true } } From 61b5b1edd8d0358cb143d618691c87b9fd54f639 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:29:30 +0200 Subject: [PATCH 3/3] =?UTF-8?q?Fix=20example=20for=20@=E2=81=A0ImportResou?= =?UTF-8?q?rce=20in=20the=20reference=20manual?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this commit, the main() method attempted to retrieve a TransferService bean from the context, but no such bean had been configured in the context. This commit addresses that by configuring a TransferService bean in the context. Closes gh-33446 --- .../java/composing-configuration-classes.adoc | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc index a30f5c26e2..d09faa734e 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc @@ -627,7 +627,7 @@ Java:: @Bean public TransferService transferService() { - return new TransferService(accountRepository()); + return new TransferServiceImpl(accountRepository()); } } ---- @@ -776,6 +776,17 @@ Java:: public DataSource dataSource() { return new DriverManagerDataSource(url, username, password); } + + @Bean + public AccountRepository accountRepository(DataSource dataSource) { + return new JdbcAccountRepository(dataSource); + } + + @Bean + public TransferService transferService(AccountRepository accountRepository) { + return new TransferServiceImpl(accountRepository); + } + } ---- @@ -800,6 +811,17 @@ Kotlin:: fun dataSource(): DataSource { return DriverManagerDataSource(url, username, password) } + + @Bean + fun accountRepository(dataSource: DataSource): AccountRepository { + return JdbcAccountRepository(dataSource) + } + + @Bean + fun transferService(accountRepository: AccountRepository): TransferService { + return TransferServiceImpl(accountRepository) + } + } ---- ======