|
20 | 20 | import java.util.Collections;
|
21 | 21 | import java.util.List;
|
22 | 22 |
|
23 |
| -import javax.annotation.PostConstruct; |
24 | 23 | import javax.sql.DataSource;
|
25 | 24 |
|
26 | 25 | import org.apache.commons.logging.Log;
|
27 | 26 | import org.apache.commons.logging.LogFactory;
|
28 | 27 |
|
29 | 28 | import org.springframework.boot.context.config.ResourceNotFoundException;
|
30 |
| -import org.springframework.context.ApplicationContext; |
31 |
| -import org.springframework.context.ApplicationListener; |
| 29 | +import org.springframework.core.io.DefaultResourceLoader; |
32 | 30 | import org.springframework.core.io.Resource;
|
| 31 | +import org.springframework.core.io.ResourceLoader; |
33 | 32 | import org.springframework.jdbc.config.SortedResourcesFactoryBean;
|
34 | 33 | import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
|
35 | 34 | import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
36 | 35 | import org.springframework.util.StringUtils;
|
37 | 36 |
|
38 | 37 | /**
|
39 |
| - * Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on |
40 |
| - * {@link PostConstruct} and {@literal data-*.sql} SQL scripts on a |
41 |
| - * {@link DataSourceInitializedEvent}. |
| 38 | + * Initialize a {@link DataSource} based on a matching {@link DataSourceProperties} |
| 39 | + * config. |
42 | 40 | *
|
43 |
| - * @author Dave Syer |
44 |
| - * @author Phillip Webb |
45 |
| - * @author Eddú Meléndez |
46 | 41 | * @author Stephane Nicoll
|
47 |
| - * @author Kazuki Shimizu |
48 |
| - * @since 1.1.0 |
49 |
| - * @see DataSourceAutoConfiguration |
| 42 | + * @since 2.0.0 |
50 | 43 | */
|
51 |
| -class DataSourceInitializer implements ApplicationListener<DataSourceInitializedEvent> { |
| 44 | +public class DataSourceInitializer { |
52 | 45 |
|
53 | 46 | private static final Log logger = LogFactory.getLog(DataSourceInitializer.class);
|
54 | 47 |
|
55 |
| - private final DataSourceProperties properties; |
56 |
| - |
57 |
| - private final ApplicationContext applicationContext; |
58 |
| - |
59 |
| - private DataSource dataSource; |
| 48 | + private final DataSource dataSource; |
60 | 49 |
|
61 |
| - private boolean initialized = false; |
| 50 | + private final DataSourceProperties properties; |
62 | 51 |
|
63 |
| - DataSourceInitializer(DataSourceProperties properties, |
64 |
| - ApplicationContext applicationContext) { |
| 52 | + private final ResourceLoader resourceLoader; |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new instance with the {@link DataSource} to initialize and its matching |
| 56 | + * {@link DataSourceProperties configuration}. |
| 57 | + * @param dataSource the datasource to initialize |
| 58 | + * @param properties the matching configuration |
| 59 | + * @param resourceLoader the resource loader to use (can be null) |
| 60 | + */ |
| 61 | + public DataSourceInitializer(DataSource dataSource, DataSourceProperties properties, |
| 62 | + ResourceLoader resourceLoader) { |
| 63 | + this.dataSource = dataSource; |
65 | 64 | this.properties = properties;
|
66 |
| - this.applicationContext = applicationContext; |
| 65 | + this.resourceLoader = (resourceLoader != null ? resourceLoader |
| 66 | + : new DefaultResourceLoader()); |
67 | 67 | }
|
68 | 68 |
|
69 |
| - @PostConstruct |
70 |
| - public void init() { |
| 69 | + /** |
| 70 | + * Create a new instance with the {@link DataSource} to initialize and its matching |
| 71 | + * {@link DataSourceProperties configuration}. |
| 72 | + * @param dataSource the datasource to initialize |
| 73 | + * @param properties the matching configuration |
| 74 | + */ |
| 75 | + public DataSourceInitializer(DataSource dataSource, DataSourceProperties properties) { |
| 76 | + this(dataSource, properties, null); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create the schema if necessary. |
| 81 | + * @see DataSourceProperties#getSchema() |
| 82 | + */ |
| 83 | + public void createSchema() { |
71 | 84 | if (!this.properties.isInitialize()) {
|
72 | 85 | logger.debug("Initialization disabled (not running DDL scripts)");
|
73 | 86 | return;
|
74 | 87 | }
|
75 |
| - if (this.applicationContext.getBeanNamesForType(DataSource.class, false, |
76 |
| - false).length > 0) { |
77 |
| - this.dataSource = this.applicationContext.getBean(DataSource.class); |
78 |
| - } |
79 |
| - if (this.dataSource == null) { |
80 |
| - logger.debug("No DataSource found so not initializing"); |
81 |
| - return; |
82 |
| - } |
83 |
| - runSchemaScripts(); |
84 |
| - } |
85 |
| - |
86 |
| - private void runSchemaScripts() { |
87 | 88 | List<Resource> scripts = getScripts("spring.datasource.schema",
|
88 | 89 | this.properties.getSchema(), "schema");
|
89 | 90 | if (!scripts.isEmpty()) {
|
90 | 91 | String username = this.properties.getSchemaUsername();
|
91 | 92 | String password = this.properties.getSchemaPassword();
|
92 | 93 | runScripts(scripts, username, password);
|
93 |
| - try { |
94 |
| - this.applicationContext |
95 |
| - .publishEvent(new DataSourceInitializedEvent(this.dataSource)); |
96 |
| - // The listener might not be registered yet, so don't rely on it. |
97 |
| - if (!this.initialized) { |
98 |
| - runDataScripts(); |
99 |
| - this.initialized = true; |
100 |
| - } |
101 |
| - } |
102 |
| - catch (IllegalStateException ex) { |
103 |
| - logger.warn("Could not send event to complete DataSource initialization (" |
104 |
| - + ex.getMessage() + ")"); |
105 |
| - } |
106 | 94 | }
|
107 | 95 | }
|
108 | 96 |
|
109 |
| - @Override |
110 |
| - public void onApplicationEvent(DataSourceInitializedEvent event) { |
| 97 | + /** |
| 98 | + * Initialize the schema if necessary. |
| 99 | + * @see DataSourceProperties#getData() |
| 100 | + */ |
| 101 | + public void initSchema() { |
111 | 102 | if (!this.properties.isInitialize()) {
|
112 | 103 | logger.debug("Initialization disabled (not running data scripts)");
|
113 | 104 | return;
|
114 | 105 | }
|
115 |
| - // NOTE the event can happen more than once and |
116 |
| - // the event datasource is not used here |
117 |
| - if (!this.initialized) { |
118 |
| - runDataScripts(); |
119 |
| - this.initialized = true; |
120 |
| - } |
121 |
| - } |
122 |
| - |
123 |
| - private void runDataScripts() { |
124 | 106 | List<Resource> scripts = getScripts("spring.datasource.data",
|
125 | 107 | this.properties.getData(), "data");
|
126 | 108 | String username = this.properties.getDataUsername();
|
@@ -159,7 +141,7 @@ else if (validate) {
|
159 | 141 | private Resource[] doGetResources(String location) {
|
160 | 142 | try {
|
161 | 143 | SortedResourcesFactoryBean factory = new SortedResourcesFactoryBean(
|
162 |
| - this.applicationContext, Collections.singletonList(location)); |
| 144 | + this.resourceLoader, Collections.singletonList(location)); |
163 | 145 | factory.afterPropertiesSet();
|
164 | 146 | return factory.getObject();
|
165 | 147 | }
|
|
0 commit comments