From a428955438c0d8d7d073e84fc570bb224f46f376 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 23 Dec 2023 13:40:12 +0100 Subject: [PATCH] Avoid unnecessary DatabasePopulator init/destroy processing Closes gh-23405 --- .../jdbc/config/DatabasePopulatorConfigUtils.java | 9 +++++++-- .../jdbc/datasource/init/DataSourceInitializer.java | 13 +++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java index f172934ba3..08d07565bc 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-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. @@ -48,6 +48,7 @@ abstract class DatabasePopulatorConfigUtils { } } + @Nullable private static BeanDefinition createDatabasePopulator(Element element, List scripts, String execution) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CompositeDatabasePopulator.class); @@ -80,8 +81,12 @@ abstract class DatabasePopulatorConfigUtils { } delegates.add(delegate.getBeanDefinition()); } - builder.addPropertyValue("populators", delegates); + if (delegates.isEmpty()) { + return null; + } + + builder.addPropertyValue("populators", delegates); return builder.getBeanDefinition(); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java index d2bb97541c..4a57474b55 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-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. @@ -58,21 +58,22 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean { } /** - * Set the {@link DatabasePopulator} to execute during the bean initialization phase. + * Set the {@link DatabasePopulator} to execute during the bean initialization phase, + * if any. * @param databasePopulator the {@code DatabasePopulator} to use during initialization * @see #setDatabaseCleaner */ - public void setDatabasePopulator(DatabasePopulator databasePopulator) { + public void setDatabasePopulator(@Nullable DatabasePopulator databasePopulator) { this.databasePopulator = databasePopulator; } /** - * Set the {@link DatabasePopulator} to execute during the bean destruction - * phase, cleaning up the database and leaving it in a known state for others. + * Set the {@link DatabasePopulator} to execute during the bean destruction phase, + * if any, cleaning up the database and leaving it in a known state for others. * @param databaseCleaner the {@code DatabasePopulator} to use during destruction * @see #setDatabasePopulator */ - public void setDatabaseCleaner(DatabasePopulator databaseCleaner) { + public void setDatabaseCleaner(@Nullable DatabasePopulator databaseCleaner) { this.databaseCleaner = databaseCleaner; }