Improve DataSourceInitializer Javadoc and implementation
This commit is contained in:
parent
87e58a6d7b
commit
c61d62ef4b
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -20,11 +20,15 @@ import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to populate a database during initialization.
|
* Used to {@linkplain #setDatabasePopulator set up} a database during
|
||||||
|
* initialization and {@link #setDatabaseCleaner clean up} a database during
|
||||||
|
* destruction.
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see DatabasePopulator
|
* @see DatabasePopulator
|
||||||
*/
|
*/
|
||||||
|
@ -40,8 +44,9 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link DataSource} to populate when this component is initialized.
|
* The {@link DataSource} for the database to populate when this component
|
||||||
* Mandatory with no default.
|
* is initialized and to clean up when this component is shut down.
|
||||||
|
* <p>This property is mandatory with no default provided.
|
||||||
* @param dataSource the DataSource
|
* @param dataSource the DataSource
|
||||||
*/
|
*/
|
||||||
public void setDataSource(DataSource dataSource) {
|
public void setDataSource(DataSource dataSource) {
|
||||||
|
@ -49,49 +54,58 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link DatabasePopulator} to use to populate the data source.
|
* Set the {@link DatabasePopulator} to execute during the bean initialization
|
||||||
* Mandatory with no default.
|
* phase.
|
||||||
* @param databasePopulator the database populator to use.
|
* @param databasePopulator the {@code DatabasePopulator} to use during
|
||||||
|
* initialization
|
||||||
|
* @see #setDatabaseCleaner
|
||||||
*/
|
*/
|
||||||
public void setDatabasePopulator(DatabasePopulator databasePopulator) {
|
public void setDatabasePopulator(DatabasePopulator databasePopulator) {
|
||||||
this.databasePopulator = databasePopulator;
|
this.databasePopulator = databasePopulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a script execution to be run in the bean destruction callback,
|
* Set the {@link DatabasePopulator} to execute during the bean destruction
|
||||||
* cleaning up the database and leaving it in a known state for others.
|
* phase, cleaning up the database and leaving it in a known state for others.
|
||||||
* @param databaseCleaner the database script executor to run on destroy
|
* @param databaseCleaner the {@code DatabasePopulator} to use during destruction
|
||||||
|
* @see #setDatabasePopulator
|
||||||
*/
|
*/
|
||||||
public void setDatabaseCleaner(DatabasePopulator databaseCleaner) {
|
public void setDatabaseCleaner(DatabasePopulator databaseCleaner) {
|
||||||
this.databaseCleaner = databaseCleaner;
|
this.databaseCleaner = databaseCleaner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flag to explicitly enable or disable the database populator.
|
* Flag to explicitly enable or disable the {@linkplain #setDatabasePopulator
|
||||||
* @param enabled true if the database populator will be called on startup
|
* database populator} and {@linkplain #setDatabaseCleaner database cleaner}.
|
||||||
|
* @param enabled {@code true} if the database populator and database cleaner
|
||||||
|
* should be called on startup and shutdown, respectively
|
||||||
*/
|
*/
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use the populator to set up data in the data source.
|
* Use the {@linkplain #setDatabasePopulator database populator} to set up
|
||||||
|
* the database.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
if (this.databasePopulator != null && this.enabled) {
|
execute(this.databasePopulator);
|
||||||
DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use the populator to clean up data in the data source.
|
* Use the {@linkplain #setDatabaseCleaner database cleaner} to clean up the
|
||||||
|
* database.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
if (this.databaseCleaner != null && this.enabled) {
|
execute(this.databaseCleaner);
|
||||||
DatabasePopulatorUtils.execute(this.databaseCleaner, this.dataSource);
|
}
|
||||||
|
|
||||||
|
private void execute(DatabasePopulator populator) {
|
||||||
|
Assert.state(dataSource != null, "DataSource must be set");
|
||||||
|
if (this.enabled && populator != null) {
|
||||||
|
DatabasePopulatorUtils.execute(populator, this.dataSource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue