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 6a4f93acd6..1903c656a8 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 @@ -578,7 +578,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; } } @@ -597,7 +597,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 } } @@ -636,14 +636,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] ====== @@ -664,7 +664,7 @@ Java:: @Bean public TransferService transferService() { - return new TransferService(accountRepository()); + return new TransferServiceImpl(accountRepository()); } } ---- @@ -697,6 +697,7 @@ The following example shows part of a sample `system-test-config.xml` file: + @@ -743,8 +744,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. @@ -755,8 +756,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. @@ -767,6 +768,7 @@ The following example shows the modified `system-test-config.xml` file: + @@ -781,13 +783,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] ====== @@ -812,6 +813,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); + } + } ---- @@ -836,21 +848,32 @@ 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) + } + } ---- ====== +.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= @@ -883,5 +906,3 @@ Kotlin:: ---- ====== - -