From eaad22dd89dbabf9fdd896956e3276a9f180b9aa Mon Sep 17 00:00:00 2001 From: Andrii Hrytsiuk Date: Fri, 16 Aug 2019 11:50:25 -0700 Subject: [PATCH 1/3] Set up SpringLiquibase beans' dependencies by type rather than name See gh-17805 --- ...yManagerFactoryDependsOnPostProcessor.java | 9 +++++ .../JdbcOperationsDependsOnPostProcessor.java | 9 +++++ ...rJdbcOperationsDependsOnPostProcessor.java | 9 +++++ .../liquibase/LiquibaseAutoConfiguration.java | 6 ++-- .../LiquibaseAutoConfigurationTests.java | 36 +++++++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java index baa90dc7de..5384222cf3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java @@ -40,4 +40,13 @@ public class EntityManagerFactoryDependsOnPostProcessor extends AbstractDependsO super(EntityManagerFactory.class, AbstractEntityManagerFactoryBean.class, dependsOn); } + /** + * Creates a new {@code EntityManagerFactoryDependsOnPostProcessor} that will set up + * dependencies upon beans with the given types. + * @param dependsOn types of the beans to depend upon + */ + public EntityManagerFactoryDependsOnPostProcessor(Class... dependsOn) { + super(EntityManagerFactory.class, AbstractEntityManagerFactoryBean.class, dependsOn); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java index c8062b32f1..abcbd5354a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java @@ -38,4 +38,13 @@ public class JdbcOperationsDependsOnPostProcessor extends AbstractDependsOnBeanF super(JdbcOperations.class, dependsOn); } + /** + * Creates a new {@code JdbcOperationsDependsOnPostProcessor} that will set up + * dependencies upon beans with the given types. + * @param dependsOn types of the beans to depend upon + */ + public JdbcOperationsDependsOnPostProcessor(Class... dependsOn) { + super(JdbcOperations.class, dependsOn); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java index a8bbc2432f..16fbe205f1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java @@ -36,4 +36,13 @@ public class NamedParameterJdbcOperationsDependsOnPostProcessor extends Abstract super(NamedParameterJdbcOperations.class, dependsOn); } + /** + * Creates a new {@code NamedParameterJdbcOperationsDependsOnPostProcessor} that will + * set up dependencies upon beans with the given types. + * @param dependsOn types of the beans to depend upon + */ + public NamedParameterJdbcOperationsDependsOnPostProcessor(Class... dependsOn) { + super(NamedParameterJdbcOperations.class, dependsOn); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 1d3df0dc08..d518b4b54d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -165,7 +165,7 @@ public class LiquibaseAutoConfiguration { protected static class LiquibaseJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor { public LiquibaseJpaDependencyConfiguration() { - super("liquibase"); + super(SpringLiquibase.class); } } @@ -180,7 +180,7 @@ public class LiquibaseAutoConfiguration { protected static class LiquibaseJdbcOperationsDependencyConfiguration extends JdbcOperationsDependsOnPostProcessor { public LiquibaseJdbcOperationsDependencyConfiguration() { - super("liquibase"); + super(SpringLiquibase.class); } } @@ -196,7 +196,7 @@ public class LiquibaseAutoConfiguration { extends NamedParameterJdbcOperationsDependsOnPostProcessor { public LiquibaseNamedParameterJdbcOperationsDependencyConfiguration() { - super("liquibase"); + super(SpringLiquibase.class); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index a99b8dc6b1..5cf9afdc73 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -32,9 +32,11 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; +import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener; @@ -300,6 +302,26 @@ public class LiquibaseAutoConfigurationTests { }); } + @Test + void userConfigurationBeans() { + this.contextRunner + .withUserConfiguration(LiquibaseUserConfiguration.class, EmbeddedDataSourceConfiguration.class) + .run((context) -> { + assertThat(context).hasBean("springLiquibase"); + assertThat(context).doesNotHaveBean("liquibase"); + }); + } + + @Test + void userConfigurationJdbcTemplateDependency() { + this.contextRunner.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class)) + .withUserConfiguration(LiquibaseUserConfiguration.class, EmbeddedDataSourceConfiguration.class) + .run((context) -> { + BeanDefinition beanDefinition = context.getBeanFactory().getBeanDefinition("jdbcTemplate"); + assertThat(beanDefinition.getDependsOn()).containsExactly("springLiquibase"); + }); + } + private ContextConsumer assertLiquibase(Consumer consumer) { return (context) -> { assertThat(context).hasSingleBean(SpringLiquibase.class); @@ -325,4 +347,18 @@ public class LiquibaseAutoConfigurationTests { } + @Configuration + static class LiquibaseUserConfiguration { + + @Bean + SpringLiquibase springLiquibase(DataSource dataSource) { + SpringLiquibase liquibase = new SpringLiquibase(); + liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml"); + liquibase.setShouldRun(true); + liquibase.setDataSource(dataSource); + return liquibase; + } + + } + } From b69f9e9fdf52d231cc7b4a2438fca667af383bdb Mon Sep 17 00:00:00 2001 From: Andrii Hrytsiuk Date: Fri, 16 Aug 2019 11:51:05 -0700 Subject: [PATCH 2/3] Add missing javadoc See gh-17805 --- .../data/jpa/EntityManagerFactoryDependsOnPostProcessor.java | 5 +++++ .../jdbc/JdbcOperationsDependsOnPostProcessor.java | 5 +++++ .../NamedParameterJdbcOperationsDependsOnPostProcessor.java | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java index 5384222cf3..32723d3c7b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java @@ -36,6 +36,11 @@ import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; */ public class EntityManagerFactoryDependsOnPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { + /** + * Creates a new {@code EntityManagerFactoryDependsOnPostProcessor} that will set up + * dependencies upon beans with the given names. + * @param dependsOn names of the beans to depend upon + */ public EntityManagerFactoryDependsOnPostProcessor(String... dependsOn) { super(EntityManagerFactory.class, AbstractEntityManagerFactoryBean.class, dependsOn); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java index abcbd5354a..cb15701cd9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java @@ -34,6 +34,11 @@ import org.springframework.jdbc.core.JdbcOperations; */ public class JdbcOperationsDependsOnPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { + /** + * Creates a new {@code JdbcOperationsDependsOnPostProcessor} that will set up + * dependencies upon beans with the given names. + * @param dependsOn names of the beans to depend upon + */ public JdbcOperationsDependsOnPostProcessor(String... dependsOn) { super(JdbcOperations.class, dependsOn); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java index 16fbe205f1..3a72a59afc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java @@ -32,6 +32,11 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; */ public class NamedParameterJdbcOperationsDependsOnPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { + /** + * Creates a new {@code NamedParameterJdbcOperationsDependsOnPostProcessor} that will + * set up dependencies upon beans with the given names. + * @param dependsOn names of the beans to depend upon + */ public NamedParameterJdbcOperationsDependsOnPostProcessor(String... dependsOn) { super(NamedParameterJdbcOperations.class, dependsOn); } From 0187e5106dfc5034b8b1650083eab37181ff0de3 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Fri, 16 Aug 2019 16:33:21 -0700 Subject: [PATCH 3/3] Polish "Set up SpringLiquibase beans' dependencies by type rather than name" See gh-17805 --- .../data/jpa/EntityManagerFactoryDependsOnPostProcessor.java | 2 ++ .../jdbc/JdbcOperationsDependsOnPostProcessor.java | 2 ++ .../NamedParameterJdbcOperationsDependsOnPostProcessor.java | 2 ++ .../liquibase/LiquibaseAutoConfigurationTests.java | 5 +++-- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java index 32723d3c7b..19a8c159ee 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EntityManagerFactoryDependsOnPostProcessor.java @@ -31,6 +31,7 @@ import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Andrii Hrytsiuk * @since 1.1.0 * @see BeanDefinition#setDependsOn(String[]) */ @@ -49,6 +50,7 @@ public class EntityManagerFactoryDependsOnPostProcessor extends AbstractDependsO * Creates a new {@code EntityManagerFactoryDependsOnPostProcessor} that will set up * dependencies upon beans with the given types. * @param dependsOn types of the beans to depend upon + * @since 2.1.8 */ public EntityManagerFactoryDependsOnPostProcessor(Class... dependsOn) { super(EntityManagerFactory.class, AbstractEntityManagerFactoryBean.class, dependsOn); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java index cb15701cd9..ef375dec4d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcOperationsDependsOnPostProcessor.java @@ -29,6 +29,7 @@ import org.springframework.jdbc.core.JdbcOperations; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Andrii Hrytsiuk * @since 2.0.4 * @see BeanDefinition#setDependsOn(String[]) */ @@ -47,6 +48,7 @@ public class JdbcOperationsDependsOnPostProcessor extends AbstractDependsOnBeanF * Creates a new {@code JdbcOperationsDependsOnPostProcessor} that will set up * dependencies upon beans with the given types. * @param dependsOn types of the beans to depend upon + * @since 2.1.8 */ public JdbcOperationsDependsOnPostProcessor(Class... dependsOn) { super(JdbcOperations.class, dependsOn); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java index 3a72a59afc..d7174a61a2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java @@ -27,6 +27,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; * beans. * * @author Dan Zheng + * @author Andrii Hrytsiuk * @since 2.1.4 * @see BeanDefinition#setDependsOn(String[]) */ @@ -45,6 +46,7 @@ public class NamedParameterJdbcOperationsDependsOnPostProcessor extends Abstract * Creates a new {@code NamedParameterJdbcOperationsDependsOnPostProcessor} that will * set up dependencies upon beans with the given types. * @param dependsOn types of the beans to depend upon + * @since 2.1.8 */ public NamedParameterJdbcOperationsDependsOnPostProcessor(Class... dependsOn) { super(NamedParameterJdbcOperations.class, dependsOn); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index 5cf9afdc73..d3c819c39f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -63,6 +63,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Stephane Nicoll * @author Dominic Gunn * @author András Deák + * @author Andrii Hrytsiuk */ public class LiquibaseAutoConfigurationTests { @@ -303,7 +304,7 @@ public class LiquibaseAutoConfigurationTests { } @Test - void userConfigurationBeans() { + public void userConfigurationBeans() { this.contextRunner .withUserConfiguration(LiquibaseUserConfiguration.class, EmbeddedDataSourceConfiguration.class) .run((context) -> { @@ -313,7 +314,7 @@ public class LiquibaseAutoConfigurationTests { } @Test - void userConfigurationJdbcTemplateDependency() { + public void userConfigurationJdbcTemplateDependency() { this.contextRunner.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class)) .withUserConfiguration(LiquibaseUserConfiguration.class, EmbeddedDataSourceConfiguration.class) .run((context) -> {