mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-24 18:29:03 +00:00
Polish contribution
Closes gh-16047
This commit is contained in:
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
* beans.
|
||||
*
|
||||
* @author Dan Zheng
|
||||
* @since 2.1.x
|
||||
* @since 2.1.4
|
||||
* @see BeanDefinition#setDependsOn(String[])
|
||||
*/
|
||||
public class NamedParameterJdbcOperationsDependsOnPostProcessor
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
|
||||
+8
-129
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 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.
|
||||
@@ -16,27 +16,15 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -47,7 +35,6 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -205,45 +192,14 @@ public class JdbcTemplateAutoConfigurationTests {
|
||||
public void testDependencyToFlywayWithJdbcTemplateMixed() {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
|
||||
.withPropertyValues("spring.flyway.locations:classpath:db/city_np")
|
||||
.withPropertyValues("spring.flyway.locations:classpath:db/city")
|
||||
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).hasNotFailed();
|
||||
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
|
||||
assertThat(context.getBean(
|
||||
NamedParameterDataSourceMigrationValidator.class).count)
|
||||
.isEqualTo(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependencyToFlywayWithOnlyNamedParameterJdbcTemplate() {
|
||||
ApplicationContextRunner contextRunner1 = new ApplicationContextRunner()
|
||||
.withPropertyValues("spring.datasource.initialization-mode=never",
|
||||
"spring.datasource.generate-unique-name=true")
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class,
|
||||
OnlyNamedParameterJdbcTemplateAutoConfiguration.class));
|
||||
contextRunner1
|
||||
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
|
||||
.withPropertyValues("spring.flyway.locations:classpath:db/city_np")
|
||||
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).hasNotFailed();
|
||||
assertThat(context.containsBean("jdbcTemplate")).isFalse();
|
||||
try {
|
||||
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
|
||||
fail("org.springframework.boot.autoconfigure.jdbc.JdcTemplate should not exist in the application context");
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
|
||||
}
|
||||
assertThat(context.getBean(NamedParameterJdbcTemplate.class))
|
||||
.isNotNull();
|
||||
assertThat(context.getBean(
|
||||
NamedParameterDataSourceMigrationValidator.class).count)
|
||||
.isEqualTo(1);
|
||||
.isEqualTo(0);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -266,7 +222,7 @@ public class JdbcTemplateAutoConfigurationTests {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
|
||||
.withPropertyValues(
|
||||
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml")
|
||||
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city.yaml")
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(LiquibaseAutoConfiguration.class))
|
||||
.run((context) -> {
|
||||
@@ -274,34 +230,7 @@ public class JdbcTemplateAutoConfigurationTests {
|
||||
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
|
||||
assertThat(context.getBean(
|
||||
NamedParameterDataSourceMigrationValidator.class).count)
|
||||
.isEqualTo(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependencyToLiquibaseWithOnlyNamedParameterJdbcTemplate() {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
|
||||
.withPropertyValues(
|
||||
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml")
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
OnlyNamedParameterJdbcTemplateAutoConfiguration.class,
|
||||
LiquibaseAutoConfiguration.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).hasNotFailed();
|
||||
assertThat(context.containsBean("jdbcTemplate")).isFalse();
|
||||
try {
|
||||
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
|
||||
fail("org.springframework.boot.autoconfigure.jdbc.JdcTemplate should not exist in the application context");
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
|
||||
}
|
||||
assertThat(context.getBean(NamedParameterJdbcTemplate.class))
|
||||
.isNotNull();
|
||||
assertThat(context.getBean(
|
||||
NamedParameterDataSourceMigrationValidator.class).count)
|
||||
.isEqualTo(1);
|
||||
.isEqualTo(0);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -390,58 +319,8 @@ public class JdbcTemplateAutoConfigurationTests {
|
||||
|
||||
NamedParameterDataSourceMigrationValidator(
|
||||
NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
||||
String sql = "SELECT COUNT(*) from CITY WHERE id = :id";
|
||||
Map<String, Long> param = new HashMap<>();
|
||||
param.put("id", 1L);
|
||||
this.count = namedParameterJdbcTemplate.queryForObject(sql, param,
|
||||
Integer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ DataSource.class })
|
||||
@ConditionalOnSingleCandidate(DataSource.class)
|
||||
@AutoConfigureAfter({ DataSourceAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class })
|
||||
@AutoConfigureBefore({ FlywayAutoConfiguration.class,
|
||||
LiquibaseAutoConfiguration.class })
|
||||
@EnableConfigurationProperties(JdbcProperties.class)
|
||||
static class OnlyNamedParameterJdbcTemplateAutoConfiguration
|
||||
implements BeanDefinitionRegistryPostProcessor {
|
||||
|
||||
@Bean
|
||||
public NamedParameterJdbcTemplate myNamedParameterJdbcTemplate(
|
||||
DataSource dataSource) {
|
||||
return new NamedParameterJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
||||
throws BeansException {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* we should remove the jdbc template bean definition to keep only
|
||||
* NamedParameterJdbcTemplate is registerd in the bean container
|
||||
* </p>
|
||||
* @param registry the bean definition registry.
|
||||
* @throws BeansException if the bean registry have any exception.
|
||||
*/
|
||||
@Override
|
||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
|
||||
throws BeansException {
|
||||
String[] excludeBeanNames = new String[] { "jdbcTemplate",
|
||||
"namedParameterJdbcTemplate" };
|
||||
for (String beanName : excludeBeanNames) {
|
||||
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
|
||||
if (beanDefinition != null) {
|
||||
registry.removeBeanDefinition(beanName);
|
||||
}
|
||||
}
|
||||
|
||||
this.count = namedParameterJdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) from CITY", Collections.emptyMap(), Integer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: 1
|
||||
author: dan-zheng
|
||||
changes:
|
||||
- createSequence:
|
||||
sequenceName: hibernate_sequence
|
||||
- createTable:
|
||||
tableName: city
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: bigint
|
||||
autoIncrement: true
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
- column:
|
||||
name: name
|
||||
type: varchar(50)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: state
|
||||
type: varchar(50)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: country
|
||||
type: varchar(50)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: map
|
||||
type: varchar(50)
|
||||
constraints:
|
||||
nullable: true
|
||||
- insert:
|
||||
tableName: city
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
value: 1
|
||||
- column:
|
||||
name: name
|
||||
value: Hangzhou
|
||||
- column:
|
||||
name: state
|
||||
value: Zhejiang
|
||||
- column:
|
||||
name: country
|
||||
value: China
|
||||
- column:
|
||||
name: map
|
||||
value: map
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
CREATE SEQUENCE HIBERNATE_SEQUENCE;
|
||||
|
||||
CREATE TABLE CITY (
|
||||
id BIGINT GENERATED BY DEFAULT AS IDENTITY,
|
||||
name VARCHAR(30),
|
||||
state VARCHAR(30),
|
||||
country VARCHAR(30),
|
||||
map VARCHAR(30)
|
||||
);
|
||||
-1
@@ -1 +0,0 @@
|
||||
INSERT INTO CITY(id, name, state, country, map) VALUES(1, 'Hangzhou', 'Zhejiang', 'China', 'map');
|
||||
Reference in New Issue
Block a user