1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
21
21
import org .springframework .beans .factory .DisposableBean ;
22
22
import org .springframework .beans .factory .InitializingBean ;
23
+ import org .springframework .util .Assert ;
23
24
24
25
/**
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.
26
29
*
27
30
* @author Dave Syer
31
+ * @author Sam Brannen
28
32
* @since 3.0
29
33
* @see DatabasePopulator
30
34
*/
@@ -40,58 +44,68 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
40
44
41
45
42
46
/**
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.
45
50
* @param dataSource the DataSource
46
51
*/
47
52
public void setDataSource (DataSource dataSource ) {
48
53
this .dataSource = dataSource ;
49
54
}
50
55
51
56
/**
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
55
62
*/
56
63
public void setDatabasePopulator (DatabasePopulator databasePopulator ) {
57
64
this .databasePopulator = databasePopulator ;
58
65
}
59
66
60
67
/**
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
64
72
*/
65
73
public void setDatabaseCleaner (DatabasePopulator databaseCleaner ) {
66
74
this .databaseCleaner = databaseCleaner ;
67
75
}
68
76
69
77
/**
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
72
82
*/
73
83
public void setEnabled (boolean enabled ) {
74
84
this .enabled = enabled ;
75
85
}
76
86
77
-
78
87
/**
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.
80
90
*/
81
91
@ Override
82
92
public void afterPropertiesSet () {
83
- if (this .databasePopulator != null && this .enabled ) {
84
- DatabasePopulatorUtils .execute (this .databasePopulator , this .dataSource );
85
- }
93
+ execute (this .databasePopulator );
86
94
}
87
95
88
96
/**
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.
90
99
*/
91
100
@ Override
92
101
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 );
95
109
}
96
110
}
97
111
0 commit comments