Skip to content

Commit 3e60ec6

Browse files
committed
Polish "Add database initializer for Spring Integration"
Closes gh-8881
1 parent 48bc29c commit 3e60ec6

File tree

5 files changed

+34
-79
lines changed

5 files changed

+34
-79
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.springframework.beans.factory.BeanFactory;
2424
import org.springframework.beans.factory.BeanFactoryAware;
2525
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
26-
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
27-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2826
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2927
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3028
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -35,16 +33,13 @@
3533
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3634
import org.springframework.context.EnvironmentAware;
3735
import org.springframework.context.annotation.Bean;
38-
import org.springframework.context.annotation.Conditional;
3936
import org.springframework.context.annotation.Configuration;
4037
import org.springframework.context.annotation.Import;
4138
import org.springframework.core.env.Environment;
4239
import org.springframework.core.io.ResourceLoader;
4340
import org.springframework.integration.config.EnableIntegration;
4441
import org.springframework.integration.config.EnableIntegrationManagement;
4542
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
46-
import org.springframework.integration.jdbc.lock.DefaultLockRepository;
47-
import org.springframework.integration.jdbc.store.JdbcChannelMessageStore;
4843
import org.springframework.integration.jdbc.store.JdbcMessageStore;
4944
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
5045
import org.springframework.integration.monitor.IntegrationMBeanExporter;
@@ -153,35 +148,12 @@ protected static class IntegrationJdbcConfiguration {
153148

154149
@Bean
155150
@ConditionalOnMissingBean
156-
@Conditional(IntegrationSchemaCondition.class)
151+
@ConditionalOnProperty(prefix = "spring.integration.jdbc.initializer", name = "enabled")
157152
public IntegrationDatabaseInitializer integrationDatabaseInitializer(
158-
DataSource dataSource, ResourceLoader resourceLoader,
159-
IntegrationProperties properties) {
153+
DataSource dataSource, ResourceLoader resourceLoader,
154+
IntegrationProperties properties) {
160155
return new IntegrationDatabaseInitializer(dataSource, resourceLoader,
161-
properties);
162-
}
163-
164-
}
165-
166-
static class IntegrationSchemaCondition extends AnyNestedCondition {
167-
168-
IntegrationSchemaCondition() {
169-
super(ConfigurationPhase.REGISTER_BEAN);
170-
}
171-
172-
@ConditionalOnBean(JdbcMessageStore.class)
173-
static class JdbcMessageStoreUsed {
174-
175-
}
176-
177-
@ConditionalOnBean(JdbcChannelMessageStore.class)
178-
static class JdbcChannelMessageStoreUsed {
179-
180-
}
181-
182-
@ConditionalOnBean(DefaultLockRepository.class)
183-
static class DefaultLockRepositoryUsed {
184-
156+
properties);
185157
}
186158

187159
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationProperties.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Configuration properties for Spring Integration.
2323
*
2424
* @author Vedran Pavic
25+
* @author Stephane Nicoll
2526
* @since 2.0.0
2627
*/
2728
@ConfigurationProperties(prefix = "spring.integration")
@@ -36,7 +37,7 @@ public Jdbc getJdbc() {
3637
public static class Jdbc {
3738

3839
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
39-
+ "integration/jdbc/schema-@@platform@@.sql";
40+
+ "integration/jdbc/schema-@@platform@@.sql";
4041

4142
/**
4243
* Path to the SQL file to use to initialize the database schema.
@@ -60,9 +61,9 @@ public Initializer getInitializer() {
6061
public class Initializer {
6162

6263
/**
63-
* Create the required integration tables on startup if necessary.
64+
* Create the required integration tables on startup.
6465
*/
65-
private boolean enabled = true;
66+
private boolean enabled = false;
6667

6768
public boolean isEnabled() {
6869
return this.enabled;

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121

2222
import javax.management.MBeanServer;
23-
import javax.sql.DataSource;
2423

2524
import org.junit.After;
2625
import org.junit.Rule;
@@ -41,7 +40,6 @@
4140
import org.springframework.integration.annotation.IntegrationComponentScan;
4241
import org.springframework.integration.annotation.MessagingGateway;
4342
import org.springframework.integration.gateway.RequestReplyExchanger;
44-
import org.springframework.integration.jdbc.store.JdbcMessageStore;
4543
import org.springframework.integration.support.channel.HeaderChannelRegistry;
4644
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
4745
import org.springframework.jdbc.BadSqlGrammarException;
@@ -147,11 +145,13 @@ public void primaryExporterIsAllowed() {
147145
}
148146

149147
@Test
150-
public void integrationJdbcDatabaseInitializerEnabledWithRequiredBeans() {
148+
public void integrationJdbcDatabaseInitializerEnabled() {
151149
load(new Class[] { EmbeddedDataSourceConfiguration.class,
152-
DataSourceTransactionManagerAutoConfiguration.class,
153-
JdbcTemplateAutoConfiguration.class,
154-
IntergrationJdbcConfiguration.class });
150+
DataSourceTransactionManagerAutoConfiguration.class,
151+
JdbcTemplateAutoConfiguration.class,
152+
IntegrationAutoConfiguration.class},
153+
"spring.datasource.generate-unique-name=true",
154+
"spring.integration.jdbc.initializer.enabled=true");
155155
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
156156
.getInitializer().isEnabled()).isTrue();
157157
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
@@ -166,44 +166,32 @@ public void integrationJdbcDatabaseInitializerEnabledWithRequiredBeans() {
166166
}
167167

168168
@Test
169-
public void integrationJdbcDatabaseInitializerDisableWithoutRequiredBeans() {
169+
public void integrationJdbcDatabaseInitializerDisabled() {
170170
load(new Class[] { EmbeddedDataSourceConfiguration.class,
171-
DataSourceTransactionManagerAutoConfiguration.class,
172-
JdbcTemplateAutoConfiguration.class });
171+
DataSourceTransactionManagerAutoConfiguration.class,
172+
JdbcTemplateAutoConfiguration.class,
173+
IntegrationAutoConfiguration.class },
174+
"spring.datasource.generate-unique-name=true",
175+
"spring.integration.jdbc.initializer.enabled=false");
173176
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
174-
.getInitializer().isEnabled()).isTrue();
177+
.getInitializer().isEnabled()).isFalse();
175178
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
176179
this.thrown.expect(BadSqlGrammarException.class);
177180
jdbcOperations.queryForList("select * from INT_MESSAGE");
178-
this.thrown.expect(BadSqlGrammarException.class);
179-
jdbcOperations.queryForList("select * from INT_GROUP_TO_MESSAGE");
180-
this.thrown.expect(BadSqlGrammarException.class);
181-
jdbcOperations.queryForList("select * from INT_MESSAGE_GROUP");
182-
this.thrown.expect(BadSqlGrammarException.class);
183-
jdbcOperations.queryForList("select * from INT_LOCK");
184-
this.thrown.expect(BadSqlGrammarException.class);
185-
jdbcOperations.queryForList("select * from INT_CHANNEL_MESSAGE");
186181
}
187182

188183
@Test
189-
public void integrationJdbcDisableDatabaseInitializer() {
184+
public void integrationJdbcDatabaseInitializerDisabledByDefault() {
190185
load(new Class[] { EmbeddedDataSourceConfiguration.class,
191-
DataSourceTransactionManagerAutoConfiguration.class,
192-
JdbcTemplateAutoConfiguration.class },
193-
"spring.integration.jdbc.initializer.enabled=false");
186+
DataSourceTransactionManagerAutoConfiguration.class,
187+
JdbcTemplateAutoConfiguration.class,
188+
IntegrationAutoConfiguration.class },
189+
"spring.datasource.generate-unique-name=true");
194190
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
195191
.getInitializer().isEnabled()).isFalse();
196192
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
197193
this.thrown.expect(BadSqlGrammarException.class);
198194
jdbcOperations.queryForList("select * from INT_MESSAGE");
199-
this.thrown.expect(BadSqlGrammarException.class);
200-
jdbcOperations.queryForList("select * from INT_GROUP_TO_MESSAGE");
201-
this.thrown.expect(BadSqlGrammarException.class);
202-
jdbcOperations.queryForList("select * from INT_MESSAGE_GROUP");
203-
this.thrown.expect(BadSqlGrammarException.class);
204-
jdbcOperations.queryForList("select * from INT_LOCK");
205-
this.thrown.expect(BadSqlGrammarException.class);
206-
jdbcOperations.queryForList("select * from INT_CHANNEL_MESSAGE");
207195
}
208196

209197
private static void assertDomains(MBeanServer mBeanServer, boolean expected,
@@ -246,16 +234,6 @@ static class IntegrationComponentScanConfiguration {
246234

247235
}
248236

249-
@Configuration
250-
static class IntergrationJdbcConfiguration {
251-
252-
@Bean
253-
public JdbcMessageStore messageStore(DataSource dataSource) {
254-
return new JdbcMessageStore(dataSource);
255-
}
256-
257-
}
258-
259237
@MessagingGateway
260238
public interface TestGateway extends RequestReplyExchanger {
261239

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ content into your application; rather pick only the properties that you need.
891891
spring.batch.table-prefix= # Table prefix for all the batch meta-data tables.
892892
893893
# SPRING INTEGRATION ({sc-spring-boot-autoconfigure}/integration/IntegrationProperties.{sc-ext}[IntegrationProperties])
894-
spring.integration.jdbc.initializer.enabled=true # Create the required integration tables on startup if necessary.
894+
spring.integration.jdbc.initializer.enabled=false # Create the required integration tables on startup.
895895
spring.integration.jdbc.schema=classpath:org/springframework/integration/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
896896
897897
# JMS ({sc-spring-boot-autoconfigure}/jms/JmsProperties.{sc-ext}[JmsProperties])

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5118,9 +5118,13 @@ Integration is available on your classpath it will be initialized through the
51185118
Spring Boot will also configure some features that are triggered by the presence of
51195119
additional Spring Integration modules. Message processing statistics will be published
51205120
over JMX if `'spring-integration-jmx'` is also on the classpath. If
5121-
`'spring-integration-jdbc'` is available on the classpath default database schema will be
5122-
initialized using `'IntegrationDatabaseInitializer'`, which can be further customized
5123-
using configuration properties.
5121+
`'spring-integration-jdbc'` is available, the default database schema can be created
5122+
on startup:
5123+
5124+
[source,properties,indent=0]
5125+
----
5126+
spring.integration.jdbc.initializer.enabled=true
5127+
----
51245128

51255129
See the
51265130
{sc-spring-boot-autoconfigure}/integration/IntegrationAutoConfiguration.{sc-ext}[`IntegrationAutoConfiguration`]

0 commit comments

Comments
 (0)