Skip to content

Commit 554bff6

Browse files
committed
Merge branch '2.1.x'
2 parents 7beb640 + 38a20f2 commit 554bff6

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
4747
import org.springframework.boot.autoconfigure.jdbc.JdbcOperationsDependsOnPostProcessor;
4848
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
49+
import org.springframework.boot.autoconfigure.jdbc.NamedParameterJdbcOperationsDependsOnPostProcessor;
4950
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
5051
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
5152
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -57,6 +58,7 @@
5758
import org.springframework.core.convert.converter.GenericConverter;
5859
import org.springframework.core.io.ResourceLoader;
5960
import org.springframework.jdbc.core.JdbcOperations;
61+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
6062
import org.springframework.jdbc.support.JdbcUtils;
6163
import org.springframework.jdbc.support.MetaDataAccessException;
6264
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
@@ -76,6 +78,7 @@
7678
* @author Jacques-Etienne Beaudet
7779
* @author Eddú Meléndez
7880
* @author Dominic Gunn
81+
* @author Dan Zheng
7982
* @since 1.1.0
8083
*/
8184
@SuppressWarnings("deprecation")
@@ -301,6 +304,23 @@ protected static class FlywayInitializerJdbcOperationsDependencyConfiguration
301304

302305
public FlywayInitializerJdbcOperationsDependencyConfiguration() {
303306
super("flywayInitializer");
307+
308+
}
309+
310+
}
311+
312+
/**
313+
* Additional configuration to ensure that {@link NamedParameterJdbcOperations}
314+
* beans depend on the {@code flywayInitializer} bean.
315+
*/
316+
@Configuration
317+
@ConditionalOnClass(NamedParameterJdbcOperations.class)
318+
@ConditionalOnBean(NamedParameterJdbcOperations.class)
319+
protected static class FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration
320+
extends NamedParameterJdbcOperationsDependsOnPostProcessor {
321+
322+
public FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration() {
323+
super("flywayInitializer");
304324
}
305325

306326
}
@@ -339,6 +359,22 @@ public FlywayJdbcOperationsDependencyConfiguration() {
339359

340360
}
341361

362+
/**
363+
* Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans
364+
* depend on the {@code flyway} bean.
365+
*/
366+
@Configuration
367+
@ConditionalOnClass(NamedParameterJdbcOperations.class)
368+
@ConditionalOnBean(NamedParameterJdbcOperations.class)
369+
protected static class FlywayNamedParameterJdbcOperationsDependencyConfiguration
370+
extends NamedParameterJdbcOperationsDependsOnPostProcessor {
371+
372+
public FlywayNamedParameterJdbcOperationsDependencyConfiguration() {
373+
super("flyway");
374+
}
375+
376+
}
377+
342378
private static class LocationResolver {
343379

344380
private static final String VENDOR_PLACEHOLDER = "{vendor}";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jdbc;
18+
19+
import org.springframework.beans.factory.config.BeanDefinition;
20+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
21+
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
22+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
23+
24+
/**
25+
* {@link BeanFactoryPostProcessor} that can be used to dynamically declare that all
26+
* {@link NamedParameterJdbcOperations} beans should "depend on" one or more specific
27+
* beans.
28+
*
29+
* @author Dan Zheng
30+
* @since 2.1.4
31+
* @see BeanDefinition#setDependsOn(String[])
32+
*/
33+
public class NamedParameterJdbcOperationsDependsOnPostProcessor
34+
extends AbstractDependsOnBeanFactoryPostProcessor {
35+
36+
public NamedParameterJdbcOperationsDependsOnPostProcessor(String... dependsOn) {
37+
super(NamedParameterJdbcOperations.class, dependsOn);
38+
}
39+
40+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3737
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
3838
import org.springframework.boot.autoconfigure.jdbc.JdbcOperationsDependsOnPostProcessor;
39+
import org.springframework.boot.autoconfigure.jdbc.NamedParameterJdbcOperationsDependsOnPostProcessor;
3940
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
4041
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4142
import org.springframework.boot.jdbc.DataSourceBuilder;
@@ -45,6 +46,7 @@
4546
import org.springframework.core.io.Resource;
4647
import org.springframework.core.io.ResourceLoader;
4748
import org.springframework.jdbc.core.JdbcOperations;
49+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
4850
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
4951
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
5052
import org.springframework.util.Assert;
@@ -58,6 +60,7 @@
5860
* @author Eddú Meléndez
5961
* @author Andy Wilkinson
6062
* @author Dominic Gunn
63+
* @author Dan Zheng
6164
* @since 1.1.0
6265
*/
6366
@Configuration
@@ -205,4 +208,20 @@ public LiquibaseJdbcOperationsDependencyConfiguration() {
205208

206209
}
207210

211+
/**
212+
* Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans
213+
* depend on the liquibase bean.
214+
*/
215+
@Configuration
216+
@ConditionalOnClass(NamedParameterJdbcOperations.class)
217+
@ConditionalOnBean(NamedParameterJdbcOperations.class)
218+
protected static class LiquibaseNamedParameterJdbcOperationsDependencyConfiguration
219+
extends NamedParameterJdbcOperationsDependsOnPostProcessor {
220+
221+
public LiquibaseNamedParameterJdbcOperationsDependencyConfiguration() {
222+
super("liquibase");
223+
}
224+
225+
}
226+
208227
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19+
import java.util.Collections;
20+
1921
import javax.sql.DataSource;
2022

2123
import org.junit.Test;
@@ -41,6 +43,7 @@
4143
* @author Dave Syer
4244
* @author Stephane Nicoll
4345
* @author Kazuki Shimizu
46+
* @author Dan Zheng
4447
*/
4548
public class JdbcTemplateAutoConfigurationTests {
4649

@@ -185,6 +188,21 @@ public void testDependencyToFlyway() {
185188
});
186189
}
187190

191+
@Test
192+
public void testDependencyToFlywayWithJdbcTemplateMixed() {
193+
this.contextRunner
194+
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
195+
.withPropertyValues("spring.flyway.locations:classpath:db/city")
196+
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
197+
.run((context) -> {
198+
assertThat(context).hasNotFailed();
199+
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
200+
assertThat(context.getBean(
201+
NamedParameterDataSourceMigrationValidator.class).count)
202+
.isEqualTo(0);
203+
});
204+
}
205+
188206
@Test
189207
public void testDependencyToLiquibase() {
190208
this.contextRunner.withUserConfiguration(DataSourceMigrationValidator.class)
@@ -199,6 +217,23 @@ public void testDependencyToLiquibase() {
199217
});
200218
}
201219

220+
@Test
221+
public void testDependencyToLiquibaseWithJdbcTemplateMixed() {
222+
this.contextRunner
223+
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
224+
.withPropertyValues(
225+
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city.yaml")
226+
.withConfiguration(
227+
AutoConfigurations.of(LiquibaseAutoConfiguration.class))
228+
.run((context) -> {
229+
assertThat(context).hasNotFailed();
230+
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
231+
assertThat(context.getBean(
232+
NamedParameterDataSourceMigrationValidator.class).count)
233+
.isEqualTo(0);
234+
});
235+
}
236+
202237
@Configuration
203238
static class CustomConfiguration {
204239

@@ -278,4 +313,16 @@ static class DataSourceMigrationValidator {
278313

279314
}
280315

316+
static class NamedParameterDataSourceMigrationValidator {
317+
318+
private final Integer count;
319+
320+
NamedParameterDataSourceMigrationValidator(
321+
NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
322+
this.count = namedParameterJdbcTemplate.queryForObject(
323+
"SELECT COUNT(*) from CITY", Collections.emptyMap(), Integer.class);
324+
}
325+
326+
}
327+
281328
}

0 commit comments

Comments
 (0)