Browse Source
Add a `TransactionManagerCustomizer` callback interface that can be used to customize auto-configured `PlatformTransactionManagers`. Also update `...transaction.*` properties under a single unified `spring.transaction...` key since the existing auto-configurations would often share a transaction manager (the technology specific transaction managers are `@ConditionalOnMissingBean` and may use a manager created by a previous auto-configuration). See gh-7561pull/7171/merge
26 changed files with 389 additions and 104 deletions
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.transaction; |
||||
|
||||
import org.springframework.transaction.PlatformTransactionManager; |
||||
|
||||
/** |
||||
* Callback interface that can be implemented by beans wishing to customize |
||||
* {@link PlatformTransactionManager PlatformTransactionManagers} whilst retaining default |
||||
* auto-configuration. |
||||
* |
||||
* @param <T> The transaction manager type |
||||
* @author Phillip Webb |
||||
*/ |
||||
|
||||
public interface PlatformTransactionManagerCustomizer<T extends PlatformTransactionManager> { |
||||
/** |
||||
* Customize the given transaction manager. |
||||
* @param transactionManager the transaction manager to customize |
||||
*/ |
||||
void customize(T transactionManager); |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.transaction; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.core.ResolvableType; |
||||
import org.springframework.transaction.PlatformTransactionManager; |
||||
|
||||
/** |
||||
* A collection of {@link PlatformTransactionManagerCustomizer}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class TransactionManagerCustomizers { |
||||
|
||||
private final List<PlatformTransactionManagerCustomizer<?>> customizers; |
||||
|
||||
public TransactionManagerCustomizers( |
||||
Collection<? extends PlatformTransactionManagerCustomizer<?>> customizers) { |
||||
this.customizers = (customizers == null ? null |
||||
: new ArrayList<PlatformTransactionManagerCustomizer<?>>(customizers)); |
||||
} |
||||
|
||||
public void customize(PlatformTransactionManager transactionManager) { |
||||
if (this.customizers != null) { |
||||
for (PlatformTransactionManagerCustomizer<?> customizer : this.customizers) { |
||||
Class<?> generic = ResolvableType |
||||
.forClass(PlatformTransactionManagerCustomizer.class, |
||||
customizer.getClass()) |
||||
.resolveGeneric(); |
||||
if (generic.isInstance(transactionManager)) { |
||||
customize(transactionManager, customizer); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" }) |
||||
private void customize(PlatformTransactionManager transactionManager, |
||||
PlatformTransactionManagerCustomizer customizer) { |
||||
customizer.customize(transactionManager); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.autoconfigure.transaction; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.transaction.PlatformTransactionManager; |
||||
import org.springframework.transaction.jta.JtaTransactionManager; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link TransactionManagerCustomizers}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class TransactionManagerCustomizersTests { |
||||
|
||||
@Test |
||||
public void customizeWithNullCustomizersShouldDoNothing() throws Exception { |
||||
new TransactionManagerCustomizers(null) |
||||
.customize(mock(PlatformTransactionManager.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void customizeShouldCheckGeneric() throws Exception { |
||||
List<TestCustomizer<?>> list = new ArrayList<TestCustomizer<?>>(); |
||||
list.add(new TestCustomizer<PlatformTransactionManager>()); |
||||
list.add(new TestJtaCustomizer()); |
||||
TransactionManagerCustomizers customizers = new TransactionManagerCustomizers( |
||||
list); |
||||
customizers.customize(mock(PlatformTransactionManager.class)); |
||||
customizers.customize(mock(JtaTransactionManager.class)); |
||||
assertThat(list.get(0).getCount()).isEqualTo(2); |
||||
assertThat(list.get(1).getCount()).isEqualTo(1); |
||||
} |
||||
|
||||
private static class TestCustomizer<T extends PlatformTransactionManager> |
||||
implements PlatformTransactionManagerCustomizer<T> { |
||||
|
||||
private int count; |
||||
|
||||
@Override |
||||
public void customize(T transactionManager) { |
||||
this.count++; |
||||
} |
||||
|
||||
public int getCount() { |
||||
return this.count; |
||||
} |
||||
|
||||
} |
||||
|
||||
private static class TestJtaCustomizer extends TestCustomizer<JtaTransactionManager> { |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue