Skip to content

Commit c61d62e

Browse files
committed
Improve DataSourceInitializer Javadoc and implementation
1 parent 87e58a6 commit c61d62e

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DataSourceInitializer.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -20,11 +20,15 @@
2020

2121
import org.springframework.beans.factory.DisposableBean;
2222
import org.springframework.beans.factory.InitializingBean;
23+
import org.springframework.util.Assert;
2324

2425
/**
25-
* Used to populate a database during initialization.
26+
* Used to {@linkplain #setDatabasePopulator set up} a database during
27+
* initialization and {@link #setDatabaseCleaner clean up} a database during
28+
* destruction.
2629
*
2730
* @author Dave Syer
31+
* @author Sam Brannen
2832
* @since 3.0
2933
* @see DatabasePopulator
3034
*/
@@ -40,58 +44,68 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
4044

4145

4246
/**
43-
* The {@link DataSource} to populate when this component is initialized.
44-
* Mandatory with no default.
47+
* The {@link DataSource} for the database to populate when this component
48+
* is initialized and to clean up when this component is shut down.
49+
* <p>This property is mandatory with no default provided.
4550
* @param dataSource the DataSource
4651
*/
4752
public void setDataSource(DataSource dataSource) {
4853
this.dataSource = dataSource;
4954
}
5055

5156
/**
52-
* The {@link DatabasePopulator} to use to populate the data source.
53-
* Mandatory with no default.
54-
* @param databasePopulator the database populator to use.
57+
* Set the {@link DatabasePopulator} to execute during the bean initialization
58+
* phase.
59+
* @param databasePopulator the {@code DatabasePopulator} to use during
60+
* initialization
61+
* @see #setDatabaseCleaner
5562
*/
5663
public void setDatabasePopulator(DatabasePopulator databasePopulator) {
5764
this.databasePopulator = databasePopulator;
5865
}
5966

6067
/**
61-
* Set a script execution to be run in the bean destruction callback,
62-
* cleaning up the database and leaving it in a known state for others.
63-
* @param databaseCleaner the database script executor to run on destroy
68+
* Set the {@link DatabasePopulator} to execute during the bean destruction
69+
* phase, cleaning up the database and leaving it in a known state for others.
70+
* @param databaseCleaner the {@code DatabasePopulator} to use during destruction
71+
* @see #setDatabasePopulator
6472
*/
6573
public void setDatabaseCleaner(DatabasePopulator databaseCleaner) {
6674
this.databaseCleaner = databaseCleaner;
6775
}
6876

6977
/**
70-
* Flag to explicitly enable or disable the database populator.
71-
* @param enabled true if the database populator will be called on startup
78+
* Flag to explicitly enable or disable the {@linkplain #setDatabasePopulator
79+
* database populator} and {@linkplain #setDatabaseCleaner database cleaner}.
80+
* @param enabled {@code true} if the database populator and database cleaner
81+
* should be called on startup and shutdown, respectively
7282
*/
7383
public void setEnabled(boolean enabled) {
7484
this.enabled = enabled;
7585
}
7686

77-
7887
/**
79-
* Use the populator to set up data in the data source.
88+
* Use the {@linkplain #setDatabasePopulator database populator} to set up
89+
* the database.
8090
*/
8191
@Override
8292
public void afterPropertiesSet() {
83-
if (this.databasePopulator != null && this.enabled) {
84-
DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
85-
}
93+
execute(this.databasePopulator);
8694
}
8795

8896
/**
89-
* Use the populator to clean up data in the data source.
97+
* Use the {@linkplain #setDatabaseCleaner database cleaner} to clean up the
98+
* database.
9099
*/
91100
@Override
92101
public void destroy() {
93-
if (this.databaseCleaner != null && this.enabled) {
94-
DatabasePopulatorUtils.execute(this.databaseCleaner, this.dataSource);
102+
execute(this.databaseCleaner);
103+
}
104+
105+
private void execute(DatabasePopulator populator) {
106+
Assert.state(dataSource != null, "DataSource must be set");
107+
if (this.enabled && populator != null) {
108+
DatabasePopulatorUtils.execute(populator, this.dataSource);
95109
}
96110
}
97111

0 commit comments

Comments
 (0)