diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index b6b8f5db97..be92343de6 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -23,6 +23,7 @@ import javax.sql.DataSource; import liquibase.integration.spring.SpringLiquibase; import liquibase.servicelocator.ServiceLocator; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -50,6 +51,7 @@ import org.springframework.util.Assert; * @author Marcel Overdijk * @author Dave Syer * @author Phillip Webb + * @author Eddú Meléndez * @since 1.1.0 */ @Configuration @@ -72,11 +74,15 @@ public class LiquibaseAutoConfiguration { private final DataSource dataSource; + private final DataSource liquibaseDataSource; + public LiquibaseConfiguration(LiquibaseProperties properties, - ResourceLoader resourceLoader, DataSource dataSource) { + ResourceLoader resourceLoader, DataSource dataSource, + @LiquibaseDataSource ObjectProvider liquibaseDataSourceProvider) { this.properties = properties; this.resourceLoader = resourceLoader; this.dataSource = dataSource; + this.liquibaseDataSource = liquibaseDataSourceProvider.getIfAvailable(); } @PostConstruct @@ -113,6 +119,9 @@ public class LiquibaseAutoConfiguration { if (this.properties.getUrl() == null) { return this.dataSource; } + else if (this.liquibaseDataSource != null) { + return this.liquibaseDataSource; + } return DataSourceBuilder.create().url(this.properties.getUrl()) .username(this.properties.getUser()) .password(this.properties.getPassword()).build(); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSource.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSource.java new file mode 100644 index 0000000000..fb7837a867 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseDataSource.java @@ -0,0 +1,41 @@ +/* + * 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.liquibase; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.beans.factory.annotation.Qualifier; + +/** + * Qualifier annotation for a DataSource to be injected in to Liquibase. If used for a second + * data source, the other (main) one would normally be marked as {@code @Primary}. + * + * @author Eddú Meléndez + * @since 1.4.1 + */ +@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, + ElementType.ANNOTATION_TYPE }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Qualifier +public @interface LiquibaseDataSource { + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index 8b3c07dc3d..94e4bee296 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.liquibase; import java.io.File; import java.util.Map; +import javax.sql.DataSource; + import liquibase.integration.spring.SpringLiquibase; import org.junit.After; import org.junit.Before; @@ -29,10 +31,13 @@ import org.junit.rules.TemporaryFolder; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.FileCopyUtils; @@ -43,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Marcel Overdijk * @author Andy Wilkinson + * @author Eddú Meléndez */ public class LiquibaseAutoConfigurationTests { @@ -241,4 +247,26 @@ public class LiquibaseAutoConfigurationTests { assertThat(content).contains("DROP TABLE PUBLIC.customer;"); } + @Test + public void testLiquibaseDateSource() { + this.context.register(LiquibaseDataSourceConfiguration.class, + EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + SpringLiquibase liquibase = this.context.getBean(SpringLiquibase.class); + assertThat(liquibase.getDataSource()).isNotNull(); + } + + @Configuration + static class LiquibaseDataSourceConfiguration { + + @LiquibaseDataSource + @Bean + public DataSource liquibaseDataSource() { + return DataSourceBuilder.create().url("jdbc:hsqldb:mem:liquibasetest") + .username("sa").build(); + } + + } + }