private void runSchemaScripts() { List<Resource> scripts = getScripts("spring.datasource.schema", this.properties.getSchema(), "schema"); if (!scripts.isEmpty()) { String username = this.properties.getSchemaUsername(); String password = this.properties.getSchemaPassword(); runScripts(scripts, username, password); try { this.applicationContext .publishEvent(new DataSourceInitializedEvent(this.dataSource)); // The listener might not be registered yet, so don't rely on it. if (!this.initialized) { runDataScripts(); this.initialized = true; } } catch (IllegalStateException ex) { logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")"); } } }
private void runSchemaScripts() { String indexStr = getIndexStr(); List<Resource> scripts = getScripts("spring.datasource" + indexStr + ".schema", this.properties.getSchema(), "schema" + indexStr); if (!scripts.isEmpty()) { String username = this.properties.getSchemaUsername(); String password = this.properties.getSchemaPassword(); runScripts(scripts, username, password); try { this.applicationContext .publishEvent(new DataSourceInitializedEvent(this.dataSource)); // The listener might not be registered yet, so don't rely on it. if (!this.initialized) { runDataScripts(); this.initialized = true; } } catch (IllegalStateException ex) { logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")"); } } }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof DataSource) { // Normally this will be the right DataSource this.dataSource = (DataSource) bean; } if (bean instanceof JpaProperties) { this.properties = (JpaProperties) bean; } if (bean instanceof EntityManagerFactory && this.dataSource != null && isInitializingDatabase()) { this.applicationContext .publishEvent(new DataSourceInitializedEvent(this.dataSource)); } return bean; }
@Override public void onApplicationEvent(DataSourceInitializedEvent event) { if (!this.properties.isInitialize()) { logger.debug("Initialization disabled (not running data scripts)"); return; } // NOTE the event can happen more than once and // the event datasource is not used here if (!this.initialized) { runDataScripts(); this.initialized = true; } }