Skip to content

Commit 561511f

Browse files
committed
Explicit notes on class/method-level semantics in class hierarchies
Issue: SPR-17445 (cherry picked from commit ea3250c)
1 parent 88f4e92 commit 561511f

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
import org.springframework.transaction.TransactionDefinition;
2828

2929
/**
30-
* Describes transaction attributes on a method or class.
30+
* Describes a transaction attribute on an individual method or on a class.
31+
*
32+
* <p>At the class level, this annotation applies as a default to all methods of
33+
* the declaring class and its subclasses. Note that it does not apply to ancestor
34+
* classes up the class hierarchy; methods need to be locally redeclared in order
35+
* to participate in a subclass-level annotation.
3136
*
3237
* <p>This annotation type is generally directly comparable to Spring's
3338
* {@link org.springframework.transaction.interceptor.RuleBasedTransactionAttribute}

src/docs/asciidoc/data-access.adoc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,17 @@ following class definition:
10841084
}
10851085
----
10861086

1087-
When the above POJO is defined as a bean in a Spring IoC container, the bean instance
1088-
can be made transactional by adding merely __one__ line of XML configuration:
1087+
Used at the class level as above, the annotation indicates a default for all methods
1088+
of the declaring class (as well as its subclasses). Alternatively, each method can
1089+
get annotated individually. Note that a class-level annotation does not apply to
1090+
ancestor classes up the class hierarchy; in such a scenario, methods need to be
1091+
locally redeclared in order to participate in a subclass-level annotation.
1092+
1093+
When a POJO class such as the one above is defined as a bean in a Spring context,
1094+
you can make the bean instance transactional through an `@EnableTransactionManagement`
1095+
annotation in a `@Configuration` class. See the javadoc for full details.
1096+
1097+
In XML configuration, the `<tx:annotation-driven/>` tag provides similar convenience:
10891098

10901099
[source,xml,indent=0]
10911100
[subs="verbatim,quotes"]
@@ -1109,6 +1118,7 @@ can be made transactional by adding merely __one__ line of XML configuration:
11091118
11101119
<!-- enable the configuration of transactional behavior based on annotations -->
11111120
__<tx:annotation-driven transaction-manager="txManager"/>__<!-- a PlatformTransactionManager is still required -->
1121+
11121122
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
11131123
<!-- (this dependency is defined somewhere else) -->
11141124
<property name="dataSource" ref="dataSource"/>
@@ -1128,13 +1138,6 @@ dependency-inject has any other name, then you have to use the `transaction-mana
11281138
attribute explicitly, as in the preceding example.
11291139
====
11301140

1131-
[NOTE]
1132-
====
1133-
The `@EnableTransactionManagement` annotation provides equivalent support if you are
1134-
using Java based configuration. Simply add the annotation to a `@Configuration` class.
1135-
See the javadocs for full details.
1136-
====
1137-
11381141
.Method visibility and @Transactional
11391142
****
11401143
When using proxies, you should apply the `@Transactional` annotation only to methods
@@ -1144,20 +1147,20 @@ method does not exhibit the configured transactional settings. Consider the use
11441147
AspectJ (see below) if you need to annotate non-public methods.
11451148
****
11461149

1147-
You can place the `@Transactional` annotation before an interface definition, a method
1150+
You can apply the `@Transactional` annotation to an interface definition, a method
11481151
on an interface, a class definition, or a __public__ method on a class. However, the
11491152
mere presence of the `@Transactional` annotation is not enough to activate the
1150-
transactional behavior. The `@Transactional` annotation is simply metadata that can be
1151-
consumed by some runtime infrastructure that is `@Transactional`-aware and that can use
1152-
the metadata to configure the appropriate beans with transactional behavior. In the
1153-
preceding example, the `<tx:annotation-driven/>` element __switches on__ the
1153+
transactional behavior. The `@Transactional` annotation is simply metadata that can
1154+
be consumed by some runtime infrastructure that is `@Transactional`-aware and that
1155+
can use the metadata to configure the appropriate beans with transactional behavior.
1156+
In the preceding example, the `<tx:annotation-driven/>` element __switches on__ the
11541157
transactional behavior.
11551158

11561159
[TIP]
11571160
====
11581161
Spring recommends that you only annotate concrete classes (and methods of concrete
1159-
classes) with the `@Transactional` annotation, as opposed to annotating interfaces. You
1160-
certainly can place the `@Transactional` annotation on an interface (or an interface
1162+
classes) with the `@Transactional` annotation, as opposed to annotating interfaces.
1163+
You certainly can place the `@Transactional` annotation on an interface (or an interface
11611164
method), but this works only as you would expect it to if you are using interface-based
11621165
proxies. The fact that Java annotations are __not inherited from interfaces__ means that
11631166
if you are using class-based proxies ( `proxy-target-class="true"`) or the weaving-based
@@ -2364,6 +2367,7 @@ class, providing SQL and any necessary parameters. The same is true for the
23642367
The `JdbcTemplate` can be used within a DAO implementation through direct instantiation
23652368
with a `DataSource` reference, or be configured in a Spring IoC container and given to
23662369
DAOs as a bean reference.
2370+
23672371
[NOTE]
23682372
====
23692373
The `DataSource` should always be configured as a bean in the Spring IoC container. In
@@ -2376,7 +2380,6 @@ corresponding to the fully qualified class name of the template instance (typica
23762380
`JdbcTemplate`, but it may be different if you are using a custom subclass of the
23772381
`JdbcTemplate` class).
23782382

2379-
23802383
[[jdbc-JdbcTemplate-examples]]
23812384
===== Examples of JdbcTemplate class usage
23822385

0 commit comments

Comments
 (0)