Compare commits
5 Commits
45f3effd19
...
5000f4262f
Author | SHA1 | Date |
---|---|---|
|
5000f4262f | |
|
7e6874ad80 | |
|
097463e3b7 | |
|
6279b6a0d1 | |
|
fe2b40c663 |
|
@ -37,18 +37,18 @@ Kotlin::
|
||||||
----
|
----
|
||||||
======
|
======
|
||||||
|
|
||||||
[NOTE]
|
[TIP]
|
||||||
====
|
====
|
||||||
As of Spring Framework 4.3, an `@Autowired` annotation on such a constructor is no longer
|
An `@Autowired` annotation on such a constructor is not necessary if the target bean
|
||||||
necessary if the target bean defines only one constructor to begin with. However, if
|
defines only one constructor. However, if several constructors are available and there is
|
||||||
several constructors are available and there is no primary/default constructor, at least
|
no primary or default constructor, at least one of the constructors must be annotated
|
||||||
one of the constructors must be annotated with `@Autowired` in order to instruct the
|
with `@Autowired` in order to instruct the container which one to use. See the discussion
|
||||||
container which one to use. See the discussion on
|
on xref:core/beans/annotation-config/autowired.adoc#beans-autowired-annotation-constructor-resolution[constructor resolution]
|
||||||
xref:core/beans/annotation-config/autowired.adoc#beans-autowired-annotation-constructor-resolution[constructor resolution] for details.
|
for details.
|
||||||
====
|
====
|
||||||
|
|
||||||
You can also apply the `@Autowired` annotation to _traditional_ setter methods,
|
You can apply the `@Autowired` annotation to _traditional_ setter methods, as the
|
||||||
as the following example shows:
|
following example shows:
|
||||||
|
|
||||||
[tabs]
|
[tabs]
|
||||||
======
|
======
|
||||||
|
@ -84,8 +84,8 @@ Kotlin::
|
||||||
----
|
----
|
||||||
======
|
======
|
||||||
|
|
||||||
You can also apply the annotation to methods with arbitrary names and multiple
|
You can apply `@Autowired` to methods with arbitrary names and multiple arguments, as the
|
||||||
arguments, as the following example shows:
|
following example shows:
|
||||||
|
|
||||||
[tabs]
|
[tabs]
|
||||||
======
|
======
|
||||||
|
@ -176,14 +176,15 @@ Kotlin::
|
||||||
====
|
====
|
||||||
Make sure that your target components (for example, `MovieCatalog` or `CustomerPreferenceDao`)
|
Make sure that your target components (for example, `MovieCatalog` or `CustomerPreferenceDao`)
|
||||||
are consistently declared by the type that you use for your `@Autowired`-annotated
|
are consistently declared by the type that you use for your `@Autowired`-annotated
|
||||||
injection points. Otherwise, injection may fail due to a "no type match found" error at runtime.
|
injection points. Otherwise, injection may fail due to a "no type match found" error at
|
||||||
|
runtime.
|
||||||
|
|
||||||
For XML-defined beans or component classes found via classpath scanning, the container
|
For XML-defined beans or component classes found via classpath scanning, the container
|
||||||
usually knows the concrete type up front. However, for `@Bean` factory methods, you need
|
usually knows the concrete type up front. However, for `@Bean` factory methods, you need
|
||||||
to make sure that the declared return type is sufficiently expressive. For components
|
to make sure that the declared return type is sufficiently expressive. For components
|
||||||
that implement several interfaces or for components potentially referred to by their
|
that implement several interfaces or for components potentially referred to by their
|
||||||
implementation type, consider declaring the most specific return type on your factory
|
implementation type, declare the most specific return type on your factory method (at
|
||||||
method (at least as specific as required by the injection points referring to your bean).
|
least as specific as required by the injection points referring to your bean).
|
||||||
====
|
====
|
||||||
|
|
||||||
.[[beans-autowired-annotation-self-injection]]Self Injection
|
.[[beans-autowired-annotation-self-injection]]Self Injection
|
||||||
|
@ -312,8 +313,8 @@ through `@Order` values in combination with `@Primary` on a single bean for each
|
||||||
====
|
====
|
||||||
|
|
||||||
Even typed `Map` instances can be autowired as long as the expected key type is `String`.
|
Even typed `Map` instances can be autowired as long as the expected key type is `String`.
|
||||||
The map values contain all beans of the expected type, and the keys contain the
|
The map values are all beans of the expected type, and the keys are the corresponding
|
||||||
corresponding bean names, as the following example shows:
|
bean names, as the following example shows:
|
||||||
|
|
||||||
[tabs]
|
[tabs]
|
||||||
======
|
======
|
||||||
|
@ -431,7 +432,7 @@ annotated constructor does not have to be public.
|
||||||
====
|
====
|
||||||
|
|
||||||
Alternatively, you can express the non-required nature of a particular dependency
|
Alternatively, you can express the non-required nature of a particular dependency
|
||||||
through Java 8's `java.util.Optional`, as the following example shows:
|
through Java's `java.util.Optional`, as the following example shows:
|
||||||
|
|
||||||
[source,java,indent=0,subs="verbatim,quotes"]
|
[source,java,indent=0,subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
|
@ -445,8 +446,8 @@ through Java 8's `java.util.Optional`, as the following example shows:
|
||||||
----
|
----
|
||||||
|
|
||||||
You can also use a parameter-level `@Nullable` annotation (of any kind in any package --
|
You can also use a parameter-level `@Nullable` annotation (of any kind in any package --
|
||||||
for example, `javax.annotation.Nullable` from JSR-305) or just leverage Kotlin built-in
|
for example, `org.jspecify.annotations.Nullable` from JSpecify) or just leverage Kotlin's
|
||||||
null-safety support:
|
built-in null-safety support:
|
||||||
|
|
||||||
[tabs]
|
[tabs]
|
||||||
======
|
======
|
||||||
|
@ -477,13 +478,6 @@ Kotlin::
|
||||||
----
|
----
|
||||||
======
|
======
|
||||||
|
|
||||||
[NOTE]
|
|
||||||
====
|
|
||||||
A type-level `@Nullable` annotation such as from JSpecify is not supported in Spring
|
|
||||||
Framework 6.2 yet. You need to upgrade to Spring Framework 7.0 where the framework
|
|
||||||
detects type-level annotations and consistently declares JSpecify in its own codebase.
|
|
||||||
====
|
|
||||||
|
|
||||||
You can also use `@Autowired` for interfaces that are well-known resolvable
|
You can also use `@Autowired` for interfaces that are well-known resolvable
|
||||||
dependencies: `BeanFactory`, `ApplicationContext`, `Environment`, `ResourceLoader`,
|
dependencies: `BeanFactory`, `ApplicationContext`, `Environment`, `ResourceLoader`,
|
||||||
`ApplicationEventPublisher`, and `MessageSource`. These interfaces and their extended
|
`ApplicationEventPublisher`, and `MessageSource`. These interfaces and their extended
|
||||||
|
@ -528,5 +522,6 @@ class MovieRecommender {
|
||||||
The `@Autowired`, `@Inject`, `@Value`, and `@Resource` annotations are handled by Spring
|
The `@Autowired`, `@Inject`, `@Value`, and `@Resource` annotations are handled by Spring
|
||||||
`BeanPostProcessor` implementations. This means that you cannot apply these annotations
|
`BeanPostProcessor` implementations. This means that you cannot apply these annotations
|
||||||
within your own `BeanPostProcessor` or `BeanFactoryPostProcessor` types (if any).
|
within your own `BeanPostProcessor` or `BeanFactoryPostProcessor` types (if any).
|
||||||
|
|
||||||
These types must be 'wired up' explicitly by using XML or a Spring `@Bean` method.
|
These types must be 'wired up' explicitly by using XML or a Spring `@Bean` method.
|
||||||
====
|
====
|
||||||
|
|
|
@ -416,7 +416,8 @@ public abstract class AbstractPlatformTransactionManager
|
||||||
"isolation level will effectively be ignored: " + def);
|
"isolation level will effectively be ignored: " + def);
|
||||||
}
|
}
|
||||||
boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
|
boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
|
||||||
return prepareTransactionStatus(def, null, true, newSynchronization, debugEnabled, null);
|
SuspendedResourcesHolder suspendedResources = suspend(null);
|
||||||
|
return prepareTransactionStatus(def, null, true, newSynchronization, debugEnabled, suspendedResources);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,11 +39,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
|
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
|
||||||
|
import static org.springframework.transaction.TransactionDefinition.ISOLATION_READ_COMMITTED;
|
||||||
import static org.springframework.transaction.TransactionDefinition.ISOLATION_REPEATABLE_READ;
|
import static org.springframework.transaction.TransactionDefinition.ISOLATION_REPEATABLE_READ;
|
||||||
import static org.springframework.transaction.TransactionDefinition.ISOLATION_SERIALIZABLE;
|
import static org.springframework.transaction.TransactionDefinition.ISOLATION_SERIALIZABLE;
|
||||||
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_MANDATORY;
|
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_MANDATORY;
|
||||||
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED;
|
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED;
|
||||||
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_SUPPORTS;
|
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_SUPPORTS;
|
||||||
|
|
||||||
|
import static org.springframework.transaction.support.AbstractPlatformTransactionManager.SYNCHRONIZATION_ALWAYS;
|
||||||
import static org.springframework.transaction.support.AbstractPlatformTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION;
|
import static org.springframework.transaction.support.AbstractPlatformTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION;
|
||||||
import static org.springframework.transaction.support.DefaultTransactionDefinition.PREFIX_ISOLATION;
|
import static org.springframework.transaction.support.DefaultTransactionDefinition.PREFIX_ISOLATION;
|
||||||
import static org.springframework.transaction.support.DefaultTransactionDefinition.PREFIX_PROPAGATION;
|
import static org.springframework.transaction.support.DefaultTransactionDefinition.PREFIX_PROPAGATION;
|
||||||
|
@ -78,6 +81,58 @@ class TransactionSupportTests {
|
||||||
.isThrownBy(() -> tm.getTransaction(new DefaultTransactionDefinition(PROPAGATION_MANDATORY)));
|
.isThrownBy(() -> tm.getTransaction(new DefaultTransactionDefinition(PROPAGATION_MANDATORY)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void noExistingTransactionWithExistingAnotherTransactionManager() {
|
||||||
|
AbstractPlatformTransactionManager tm1 = new TestTransactionManager(false, true);
|
||||||
|
tm1.setTransactionSynchronization(SYNCHRONIZATION_ALWAYS);
|
||||||
|
|
||||||
|
DefaultTransactionDefinition txDef1 =
|
||||||
|
new DefaultTransactionDefinition(PROPAGATION_REQUIRED);
|
||||||
|
txDef1.setName("tx1");
|
||||||
|
txDef1.setReadOnly(false);
|
||||||
|
txDef1.setIsolationLevel(ISOLATION_READ_COMMITTED);
|
||||||
|
|
||||||
|
DefaultTransactionStatus txStatus1 = (DefaultTransactionStatus)tm1.getTransaction(txDef1);
|
||||||
|
|
||||||
|
// assert for txStatus1 and TransactionSynchronizationManager property
|
||||||
|
assertThat(txStatus1.hasTransaction()).as("Must have transaction").isTrue();
|
||||||
|
assertThat(txStatus1.isNewTransaction()).as("Must be new transaction").isTrue();
|
||||||
|
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly())
|
||||||
|
.as("Transaction1 Must be readOnly false")
|
||||||
|
.isEqualTo(txStatus1.isReadOnly());
|
||||||
|
assertThat(TransactionSynchronizationManager.getCurrentTransactionName())
|
||||||
|
.as("TransactionSynchronizationManager have correct transaction name")
|
||||||
|
.isEqualTo(txStatus1.getTransactionName());
|
||||||
|
assertThat(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel())
|
||||||
|
.as("isolation level must be default").isNull();
|
||||||
|
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
|
||||||
|
|
||||||
|
// Setting another trnasaction manager
|
||||||
|
AbstractPlatformTransactionManager tm2 = new TestTransactionManager(false, true);
|
||||||
|
tm2.setTransactionSynchronization(SYNCHRONIZATION_ALWAYS);
|
||||||
|
|
||||||
|
// Opening a new transaction before `transaction 1` commits.
|
||||||
|
DefaultTransactionDefinition txDef2 =
|
||||||
|
new DefaultTransactionDefinition(PROPAGATION_SUPPORTS);
|
||||||
|
txDef2.setReadOnly(true);
|
||||||
|
txDef2.setIsolationLevel(ISOLATION_REPEATABLE_READ);
|
||||||
|
txDef2.setName("tx2");
|
||||||
|
|
||||||
|
// assert for txStatus1 and TransactionSynchronizationManager property
|
||||||
|
DefaultTransactionStatus txStatus2 = (DefaultTransactionStatus)
|
||||||
|
tm2.getTransaction(txDef2);
|
||||||
|
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).as("Must not have transaction")
|
||||||
|
.isEqualTo(txStatus2.hasTransaction());
|
||||||
|
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).as("Must be readOnly true")
|
||||||
|
.isEqualTo(txStatus2.isReadOnly());
|
||||||
|
assertThat(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel())
|
||||||
|
.as("isolation level must be Repeatable Read")
|
||||||
|
.isEqualTo(ISOLATION_REPEATABLE_READ);
|
||||||
|
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
|
||||||
|
|
||||||
|
TransactionSynchronizationManager.clearSynchronization();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void existingTransaction() {
|
void existingTransaction() {
|
||||||
PlatformTransactionManager tm = new TestTransactionManager(true, true);
|
PlatformTransactionManager tm = new TestTransactionManager(true, true);
|
||||||
|
|
Loading…
Reference in New Issue