From ce3ef4666bd8e79ef0ff9d8b6e8f868ebd5a3fec Mon Sep 17 00:00:00 2001 From: jisub-dev Date: Fri, 13 Feb 2026 23:32:11 +0900 Subject: [PATCH] Transaction docs: add programmatic configuration example - Extract code examples to separate Java, Kotlin, and XML files - Add Kotlin configuration sample alongside Java - Change "Java Config" terminology to "Programmatic Configuration" - Use include-code directive for better maintainability Addresses review feedback in gh-36323 Signed-off-by: jisub-dev --- .../transaction/declarative/annotations.adoc | 61 ++++++++----------- .../declarative/annotations/AppConfig.java | 42 +++++++++++++ .../declarative/annotations/AppConfig.kt | 41 +++++++++++++ .../annotations/annotations-tx.xml | 30 +++++++++ 4 files changed, 139 insertions(+), 35 deletions(-) create mode 100644 framework-docs/src/main/java/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.kt create mode 100644 framework-docs/src/main/resources/org/springframework/docs/dataaccess/transaction/declarative/annotations/annotations-tx.xml diff --git a/framework-docs/modules/ROOT/pages/data-access/transaction/declarative/annotations.adoc b/framework-docs/modules/ROOT/pages/data-access/transaction/declarative/annotations.adoc index 78ea7c8b328c..d37ebe589074 100644 --- a/framework-docs/modules/ROOT/pages/data-access/transaction/declarative/annotations.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/transaction/declarative/annotations.adoc @@ -89,47 +89,38 @@ annotation in a `@Configuration` class. See the {spring-framework-api}/transaction/annotation/EnableTransactionManagement.html[javadoc] for full details. -In XML configuration, the `` tag provides similar convenience: +The following examples show the configuration needed to enable annotation-driven transaction management: -[source,xml,indent=0,subs="verbatim,quotes"] +[tabs] +====== +Java:: ++ +[source,java,indent=0,subs="verbatim,quotes",role="primary"] +---- +include-code::AppConfig[] ---- - - - - - - - - - - <1> - - - - - - +Kotlin:: ++ +[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] +---- +include-code::AppConfig[] +---- - +XML:: ++ +[source,xml,indent=0,subs="verbatim,quotes",role="secondary"] +---- +include-code::annotations-tx[tags=snippet] ---- -<1> The line that makes the bean instance transactional. +====== -TIP: You can omit the `transaction-manager` attribute in the `` -tag if the bean name of the `TransactionManager` that you want to wire in has the name -`transactionManager`. If the `TransactionManager` bean that you want to dependency-inject -has any other name, you have to use the `transaction-manager` attribute, as in the -preceding example. +TIP: In programmatic configuration, the `@EnableTransactionManagement` annotation uses any +`PlatformTransactionManager` bean in the context. In XML configuration, you can omit +the `transaction-manager` attribute in the `` tag if the bean +name of the `TransactionManager` that you want to wire in has the name `transactionManager`. +If the `TransactionManager` bean has any other name, you have to use the +`transaction-manager` attribute explicitly, as in the preceding example. Reactive transactional methods use reactive return types in contrast to imperative programming arrangements as the following listing shows: diff --git a/framework-docs/src/main/java/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.java b/framework-docs/src/main/java/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.java new file mode 100644 index 000000000000..e0a22ef02090 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.transaction.declarative.annotations; + +import javax.sql.DataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +// tag::snippet[] +@Configuration +@EnableTransactionManagement +public class AppConfig { + + @Bean + public FooService fooService() { + return new DefaultFooService(); + } + + @Bean + public PlatformTransactionManager txManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.kt b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.kt new file mode 100644 index 000000000000..6b98477bb31f --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/dataaccess/transaction/declarative/annotations/AppConfig.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2002-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.dataaccess.transaction.declarative.annotations + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.jdbc.datasource.DataSourceTransactionManager +import org.springframework.transaction.PlatformTransactionManager +import org.springframework.transaction.annotation.EnableTransactionManagement +import javax.sql.DataSource + +// tag::snippet[] +@Configuration +@EnableTransactionManagement +class AppConfig { + + @Bean + fun fooService(): FooService { + return DefaultFooService() + } + + @Bean + fun txManager(dataSource: DataSource): PlatformTransactionManager { + return DataSourceTransactionManager(dataSource) + } +} +// end::snippet[] diff --git a/framework-docs/src/main/resources/org/springframework/docs/dataaccess/transaction/declarative/annotations/annotations-tx.xml b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/transaction/declarative/annotations/annotations-tx.xml new file mode 100644 index 000000000000..ffc46184be07 --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/dataaccess/transaction/declarative/annotations/annotations-tx.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + +