diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java index 837372f862..38cad56487 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java @@ -113,7 +113,7 @@ public class Neo4jDataAutoConfiguration { public Neo4jTransactionManager transactionManager(Driver driver, DatabaseSelectionProvider databaseNameProvider, ObjectProvider optionalCustomizers) { Neo4jTransactionManager transactionManager = new Neo4jTransactionManager(driver, databaseNameProvider); - optionalCustomizers.ifAvailable((customizer) -> customizer.customize(transactionManager)); + optionalCustomizers.ifAvailable((customizer) -> customizer.customize((TransactionManager) transactionManager)); return transactionManager; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java index 55bbd9cfeb..b0741f09d2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java @@ -63,7 +63,8 @@ public class DataSourceTransactionManagerAutoConfiguration { DataSourceTransactionManager transactionManager(Environment environment, DataSource dataSource, ObjectProvider transactionManagerCustomizers) { DataSourceTransactionManager transactionManager = createTransactionManager(environment, dataSource); - transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager)); + transactionManagerCustomizers + .ifAvailable((customizers) -> customizers.customize((TransactionManager) transactionManager)); return transactionManager; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java index 0520ff0948..90355acc66 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java @@ -93,7 +93,8 @@ public abstract class JpaBaseConfiguration { public PlatformTransactionManager transactionManager( ObjectProvider transactionManagerCustomizers) { JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager)); + transactionManagerCustomizers + .ifAvailable((customizers) -> customizers.customize((TransactionManager) transactionManager)); return transactionManager; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/PlatformTransactionManagerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/PlatformTransactionManagerCustomizer.java index 64c7fd927b..b2e2eb78ae 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/PlatformTransactionManagerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/PlatformTransactionManagerCustomizer.java @@ -26,14 +26,12 @@ import org.springframework.transaction.PlatformTransactionManager; * @param the transaction manager type * @author Phillip Webb * @since 1.5.0 + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of + * {@link TransactionManagerCustomizer}. */ +@Deprecated(since = "3.2.0", forRemoval = true) @FunctionalInterface -public interface PlatformTransactionManagerCustomizer { - - /** - * Customize the given transaction manager. - * @param transactionManager the transaction manager to customize - */ - void customize(T transactionManager); +public interface PlatformTransactionManagerCustomizer + extends TransactionManagerCustomizer { } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfiguration.java index 692123bae4..8106fee224 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfiguration.java @@ -39,8 +39,8 @@ public class TransactionManagerCustomizationAutoConfiguration { @Bean @ConditionalOnMissingBean TransactionManagerCustomizers platformTransactionManagerCustomizers( - ObjectProvider> customizers) { - return new TransactionManagerCustomizers(customizers.orderedStream().toList()); + ObjectProvider> customizers) { + return TransactionManagerCustomizers.of(customizers.orderedStream().toList()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizer.java new file mode 100644 index 0000000000..e268fe87a4 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizer.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2023 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.boot.autoconfigure.transaction; + +import org.springframework.transaction.TransactionManager; + +/** + * Callback interface that can be implemented by beans wishing to customize + * {@link TransactionManager TransactionManagers} while retaining default + * auto-configuration. + * + * @param the transaction manager type + * @author Andy Wilkinson + * @since 3.2.0 + */ +public interface TransactionManagerCustomizer { + + /** + * Customize the given transaction manager. + * @param transactionManager the transaction manager to customize + */ + void customize(T transactionManager); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java index 2bb603c4bc..88f513a3c9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java @@ -23,26 +23,69 @@ import java.util.List; import org.springframework.boot.util.LambdaSafe; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionManager; /** - * A collection of {@link PlatformTransactionManagerCustomizer}. + * A collection of {@link TransactionManagerCustomizer TransactionManagerCustomizers}. * * @author Phillip Webb + * @author Andy Wilkinson * @since 1.5.0 */ public class TransactionManagerCustomizers { - private final List> customizers; + private final List> customizers; + /** + * Creates a new {@code TransactionManagerCustomizers} instance containing the given + * {@code customizers}. + * @param customizers the customizers + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of {@link #of(Collection)} + */ + @SuppressWarnings("removal") + @Deprecated(since = "3.2.0", forRemoval = true) public TransactionManagerCustomizers(Collection> customizers) { - this.customizers = (customizers != null) ? new ArrayList<>(customizers) : Collections.emptyList(); + this((customizers != null) ? new ArrayList<>(customizers) + : Collections.>emptyList()); } + private TransactionManagerCustomizers(List> customizers) { + this.customizers = customizers; + } + + /** + * Customize the given {@code platformTransactionManager}. + * @param platformTransactionManager the platform transaction manager to customize + * @deprecated since 3.2.0 for removal in 3.4.0 in favor of + * {@link #customize(TransactionManager)} + */ + @Deprecated(since = "3.2.0", forRemoval = true) + public void customize(PlatformTransactionManager platformTransactionManager) { + customize((TransactionManager) platformTransactionManager); + } + + /** + * Customize the given {@code transactionManager}. + * @param transactionManager the transaction manager to customize + * @since 3.2.0 + */ @SuppressWarnings("unchecked") - public void customize(PlatformTransactionManager transactionManager) { - LambdaSafe.callbacks(PlatformTransactionManagerCustomizer.class, this.customizers, transactionManager) + public void customize(TransactionManager transactionManager) { + LambdaSafe.callbacks(TransactionManagerCustomizer.class, this.customizers, transactionManager) .withLogger(TransactionManagerCustomizers.class) .invoke((customizer) -> customizer.customize(transactionManager)); } + /** + * Returns a new {@code TransactionManagerCustomizers} instance containing the given + * {@code customizers}. + * @param customizers the customizers + * @return the new instance + * @since 3.2.0 + */ + public static TransactionManagerCustomizers of(Collection> customizers) { + return new TransactionManagerCustomizers((customizers != null) ? new ArrayList<>(customizers) + : Collections.>emptyList()); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionProperties.java index d48200a06f..808f281879 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionProperties.java @@ -32,7 +32,7 @@ import org.springframework.transaction.support.AbstractPlatformTransactionManage * @since 1.5.0 */ @ConfigurationProperties(prefix = "spring.transaction") -public class TransactionProperties implements PlatformTransactionManagerCustomizer { +public class TransactionProperties implements TransactionManagerCustomizer { /** * Default transaction timeout. If a duration suffix is not specified, seconds will be diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/jta/JndiJtaConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/jta/JndiJtaConfiguration.java index 91b59dd02a..075a2c6816 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/jta/JndiJtaConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/jta/JndiJtaConfiguration.java @@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.TransactionManager; import org.springframework.transaction.jta.JtaTransactionManager; /** @@ -43,7 +44,8 @@ class JndiJtaConfiguration { JtaTransactionManager transactionManager( ObjectProvider transactionManagerCustomizers) { JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(); - transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(jtaTransactionManager)); + transactionManagerCustomizers + .ifAvailable((customizers) -> customizers.customize((TransactionManager) jtaTransactionManager)); return jtaTransactionManager; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java index 100f98b045..b5c385699f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java @@ -40,7 +40,8 @@ import jakarta.transaction.UserTransaction; import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy; import org.hibernate.boot.model.naming.ImplicitNamingStrategy; import org.hibernate.boot.model.naming.PhysicalNamingStrategy; -import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.ManagedBeanSettings; +import org.hibernate.cfg.SchemaToolingSettings; import org.hibernate.dialect.H2Dialect; import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform; import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; @@ -129,7 +130,8 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes void testDmlScriptRunsEarly() { contextRunner().withUserConfiguration(TestInitializedJpaConfiguration.class) .withClassLoader(new HideDataScriptClassLoader()) - .withPropertyValues("spring.jpa.show-sql=true", "spring.jpa.hibernate.ddl-auto:create-drop", + .withPropertyValues("spring.jpa.show-sql=true", "spring.jpa.properties.hibernate.format_sql=true", + "spring.jpa.properties.hibernate.highlight_sql=true", "spring.jpa.hibernate.ddl-auto:create-drop", "spring.sql.init.data-locations:/city.sql", "spring.jpa.defer-datasource-initialization=true") .run((context) -> assertThat(context.getBean(TestInitializedJpaConfiguration.class).called).isTrue()); } @@ -386,8 +388,8 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes @Test void vendorPropertiesWithEmbeddedDatabaseAndNoDdlProperty() { contextRunner().run(vendorProperties((vendorProperties) -> { - assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION); - assertThat(vendorProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, "create-drop"); + assertThat(vendorProperties).doesNotContainKeys(SchemaToolingSettings.JAKARTA_HBM2DDL_DATABASE_ACTION); + assertThat(vendorProperties).containsEntry(SchemaToolingSettings.HBM2DDL_AUTO, "create-drop"); })); } @@ -395,8 +397,8 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes void vendorPropertiesWhenDdlAutoPropertyIsSet() { contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=update") .run(vendorProperties((vendorProperties) -> { - assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION); - assertThat(vendorProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, "update"); + assertThat(vendorProperties).doesNotContainKeys(SchemaToolingSettings.JAKARTA_HBM2DDL_DATABASE_ACTION); + assertThat(vendorProperties).containsEntry(SchemaToolingSettings.HBM2DDL_AUTO, "update"); })); } @@ -406,8 +408,8 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes .withPropertyValues("spring.jpa.hibernate.ddl-auto=update", "spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop") .run(vendorProperties((vendorProperties) -> { - assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION); - assertThat(vendorProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, "create-drop"); + assertThat(vendorProperties).doesNotContainKeys(SchemaToolingSettings.JAKARTA_HBM2DDL_DATABASE_ACTION); + assertThat(vendorProperties).containsEntry(SchemaToolingSettings.HBM2DDL_AUTO, "create-drop"); })); } @@ -415,7 +417,7 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes void vendorPropertiesWhenDdlAutoPropertyIsSetToNone() { contextRunner().withPropertyValues("spring.jpa.hibernate.ddl-auto=none") .run(vendorProperties((vendorProperties) -> assertThat(vendorProperties).doesNotContainKeys( - AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, AvailableSettings.HBM2DDL_AUTO))); + SchemaToolingSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, SchemaToolingSettings.HBM2DDL_AUTO))); } @Test @@ -423,8 +425,9 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes contextRunner() .withPropertyValues("spring.jpa.properties.jakarta.persistence.schema-generation.database.action=create") .run(vendorProperties((vendorProperties) -> { - assertThat(vendorProperties).containsEntry(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, "create"); - assertThat(vendorProperties).doesNotContainKeys(AvailableSettings.HBM2DDL_AUTO); + assertThat(vendorProperties).containsEntry(SchemaToolingSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, + "create"); + assertThat(vendorProperties).doesNotContainKeys(SchemaToolingSettings.HBM2DDL_AUTO); })); } @@ -434,8 +437,9 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes .withPropertyValues("spring.jpa.properties.jakarta.persistence.schema-generation.database.action=create", "spring.jpa.hibernate.ddl-auto=create-only") .run(vendorProperties((vendorProperties) -> { - assertThat(vendorProperties).containsEntry(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, "create"); - assertThat(vendorProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, "create-only"); + assertThat(vendorProperties).containsEntry(SchemaToolingSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, + "create"); + assertThat(vendorProperties).containsEntry(SchemaToolingSettings.HBM2DDL_AUTO, "create-only"); })); } @@ -570,7 +574,7 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes @Bean HibernatePropertiesCustomizer disableBeanContainerHibernatePropertiesCustomizer() { - return (hibernateProperties) -> hibernateProperties.remove(AvailableSettings.BEAN_CONTAINER); + return (hibernateProperties) -> hibernateProperties.remove(ManagedBeanSettings.BEAN_CONTAINER); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfigurationTests.java index 9fe13d8bf0..7641ddcdf7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizationAutoConfigurationTests.java @@ -62,7 +62,7 @@ class TransactionManagerCustomizationAutoConfigurationTests { @Bean TransactionManagerCustomizers customTransactionManagerCustomizers() { - return new TransactionManagerCustomizers(Collections.emptyList()); + return TransactionManagerCustomizers.of(Collections.>emptyList()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizersTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizersTests.java index 9f58278137..396b009876 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizersTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizersTests.java @@ -22,6 +22,7 @@ import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionManager; import org.springframework.transaction.jta.JtaTransactionManager; import static org.assertj.core.api.Assertions.assertThat; @@ -36,7 +37,7 @@ class TransactionManagerCustomizersTests { @Test void customizeWithNullCustomizersShouldDoNothing() { - new TransactionManagerCustomizers(null).customize(mock(PlatformTransactionManager.class)); + TransactionManagerCustomizers.of(null).customize(mock(TransactionManager.class)); } @Test @@ -44,15 +45,14 @@ class TransactionManagerCustomizersTests { List> list = new ArrayList<>(); list.add(new TestCustomizer<>()); list.add(new TestJtaCustomizer()); - TransactionManagerCustomizers customizers = new TransactionManagerCustomizers(list); - customizers.customize(mock(PlatformTransactionManager.class)); - customizers.customize(mock(JtaTransactionManager.class)); + TransactionManagerCustomizers customizers = TransactionManagerCustomizers.of(list); + customizers.customize((TransactionManager) mock(PlatformTransactionManager.class)); + customizers.customize((TransactionManager) mock(JtaTransactionManager.class)); assertThat(list.get(0).getCount()).isEqualTo(2); assertThat(list.get(1).getCount()).isOne(); } - static class TestCustomizer - implements PlatformTransactionManagerCustomizer { + static class TestCustomizer implements TransactionManagerCustomizer { private int count;