Merge branch '6.1.x'

This commit is contained in:
Sam Brannen 2024-08-29 16:30:26 +02:00
commit a2b7b1c8af
1 changed files with 42 additions and 21 deletions

View File

@ -578,7 +578,7 @@ Java::
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName()); MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) { if (attrs != null) {
for (Object value : attrs.get("value")) { for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) { if (context.getEnvironment().matchesProfiles((String[]) value)) {
return true; return true;
} }
} }
@ -597,7 +597,7 @@ Kotlin::
val attrs = metadata.getAllAnnotationAttributes(Profile::class.java.name) val attrs = metadata.getAllAnnotationAttributes(Profile::class.java.name)
if (attrs != null) { if (attrs != null) {
for (value in attrs["value"]!!) { for (value in attrs["value"]!!) {
if (context.environment.acceptsProfiles(Profiles.of(*value as Array<String>))) { if (context.environment.matchesProfiles(*value as Array<String>)) {
return true 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]] [[beans-java-combining-xml-centric-declare-as-bean]]
==== Declaring `@Configuration` classes as plain Spring `<bean/>` elements ==== Declaring `@Configuration` classes as plain Spring `<bean/>` elements
Remember that `@Configuration` classes are ultimately bean definitions in the Remember that `@Configuration` classes are ultimately bean definitions in the container.
container. In this series examples, we create a `@Configuration` class named `AppConfig` and In this series of examples, we create a `@Configuration` class named `AppConfig` and
include it within `system-test-config.xml` as a `<bean/>` definition. Because include it within `system-test-config.xml` as a `<bean/>` definition. Because
`<context:annotation-config/>` is switched on, the container recognizes the `<context:annotation-config/>` is switched on, the container recognizes the
`@Configuration` annotation and processes the `@Bean` methods declared in `AppConfig` `@Configuration` annotation and processes the `@Bean` methods declared in `AppConfig`
properly. properly.
The following example shows an ordinary configuration class in Java: The following example shows the `AppConfig` configuration class in Java and Kotlin:
[tabs] [tabs]
====== ======
@ -664,7 +664,7 @@ Java::
@Bean @Bean
public TransferService transferService() { 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:
<beans> <beans>
<!-- enable processing of annotations such as @Autowired and @Configuration --> <!-- enable processing of annotations such as @Autowired and @Configuration -->
<context:annotation-config/> <context:annotation-config/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/> <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="com.acme.AppConfig"/> <bean class="com.acme.AppConfig"/>
@ -743,8 +744,8 @@ Kotlin::
---- ----
====== ======
NOTE: In `system-test-config.xml` file, the `AppConfig` `<bean/>` does not declare an `id` NOTE: In the `system-test-config.xml` file, the `AppConfig` `<bean/>` does not declare an `id`
element. While it would be acceptable to do so, it is unnecessary, given that no other bean 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. 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` Similarly, the `DataSource` bean is only ever autowired by type, so an explicit bean `id`
is not strictly required. is not strictly required.
@ -755,8 +756,8 @@ is not strictly required.
Because `@Configuration` is meta-annotated with `@Component`, `@Configuration`-annotated Because `@Configuration` is meta-annotated with `@Component`, `@Configuration`-annotated
classes are automatically candidates for component scanning. Using the same scenario as 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. described in the previous example, we can redefine `system-test-config.xml` to take
Note that, in this case, we need not explicitly declare advantage of component-scanning. Note that, in this case, we need not explicitly declare
`<context:annotation-config/>`, because `<context:component-scan/>` enables the same `<context:annotation-config/>`, because `<context:component-scan/>` enables the same
functionality. functionality.
@ -767,6 +768,7 @@ The following example shows the modified `system-test-config.xml` file:
<beans> <beans>
<!-- picks up and registers AppConfig as a bean definition --> <!-- picks up and registers AppConfig as a bean definition -->
<context:component-scan base-package="com.acme"/> <context:component-scan base-package="com.acme"/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/> <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
@ -781,13 +783,12 @@ The following example shows the modified `system-test-config.xml` file:
=== `@Configuration` Class-centric Use of XML with `@ImportResource` === `@Configuration` Class-centric Use of XML with `@ImportResource`
In applications where `@Configuration` classes are the primary mechanism for configuring 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 the container, it may still be necessary to use at least some XML. In such scenarios, you
scenarios, you can use `@ImportResource` and define only as much XML as you need. Doing can use `@ImportResource` and define only as much XML as you need. Doing so achieves a
so achieves a "`Java-centric`" approach to configuring the container and keeps XML to a "`Java-centric`" approach to configuring the container and keeps XML to a bare minimum.
bare minimum. The following example (which includes a configuration class, an XML file The following example (which includes a configuration class, an XML file that defines a
that defines a bean, a properties file, and the `main` class) shows how to use bean, a properties file, and the `main()` method) shows how to use the `@ImportResource`
the `@ImportResource` annotation to achieve "`Java-centric`" configuration that uses XML annotation to achieve "`Java-centric`" configuration that uses XML as needed:
as needed:
[tabs] [tabs]
====== ======
@ -812,6 +813,17 @@ Java::
public DataSource dataSource() { public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password); 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 { fun dataSource(): DataSource {
return DriverManagerDataSource(url, username, password) 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"] [source,xml,indent=0,subs="verbatim,quotes"]
---- ----
properties-config.xml
<beans> <beans>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/> <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
</beans> </beans>
---- ----
.jdbc.properties
[literal,subs="verbatim,quotes"] [literal,subs="verbatim,quotes"]
---- ----
jdbc.properties
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
jdbc.username=sa jdbc.username=sa
jdbc.password= jdbc.password=
@ -883,5 +906,3 @@ Kotlin::
---- ----
====== ======