forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the scope of DataSourceInitializer
This commit separates the lifecycle of the datasource initialization from DataSourceInitializer itself. It also makes sure that a @primary data source is no longer required. See spring-projectsgh-9528
- Loading branch information
Showing
7 changed files
with
304 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...va/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2012-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.jdbc; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.GenericBeanDefinition; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
/** | ||
* Configures DataSource initialization. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
@Configuration | ||
@Import({ DataSourceInitializerInvoker.class, DataSourceInitializationConfiguration.Registrar.class }) | ||
class DataSourceInitializationConfiguration { | ||
|
||
/** | ||
* {@link ImportBeanDefinitionRegistrar} to register the | ||
* {@link DataSourceInitializerPostProcessor} without causing early bean instantiation | ||
* issues. | ||
*/ | ||
static class Registrar implements ImportBeanDefinitionRegistrar { | ||
|
||
private static final String BEAN_NAME = "dataSourceInitializerPostProcessor"; | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, | ||
BeanDefinitionRegistry registry) { | ||
if (!registry.containsBeanDefinition(BEAN_NAME)) { | ||
GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); | ||
beanDefinition.setBeanClass(DataSourceInitializerPostProcessor.class); | ||
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); | ||
// We don't need this one to be post processed otherwise it can cause a | ||
// cascade of bean instantiation that we would rather avoid. | ||
beanDefinition.setSynthetic(true); | ||
registry.registerBeanDefinition(BEAN_NAME, beanDefinition); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...c/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2012-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.jdbc; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationListener; | ||
|
||
/** | ||
* Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on | ||
* {@link InitializingBean#afterPropertiesSet()} and {@literal data-*.sql} SQL scripts on | ||
* a {@link DataSourceInitializedEvent}. | ||
* | ||
* @author Stephane Nicoll | ||
* @see DataSourceAutoConfiguration | ||
*/ | ||
class DataSourceInitializerInvoker | ||
implements ApplicationListener<DataSourceInitializedEvent>, InitializingBean { | ||
|
||
private static final Log logger = LogFactory.getLog(DataSourceInitializerInvoker.class); | ||
|
||
private final ObjectProvider<DataSource> dataSource; | ||
|
||
private final DataSourceProperties properties; | ||
|
||
private final ApplicationContext applicationContext; | ||
|
||
private DataSourceInitializer dataSourceInitializer; | ||
|
||
private boolean initialized; | ||
|
||
DataSourceInitializerInvoker(ObjectProvider<DataSource> dataSource, | ||
DataSourceProperties properties, | ||
ApplicationContext applicationContext) { | ||
this.dataSource = dataSource; | ||
this.properties = properties; | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
DataSourceInitializer initializer = getDataSourceInitializer(); | ||
if (initializer != null) { | ||
boolean schemaCreated = this.dataSourceInitializer.createSchema(); | ||
if (schemaCreated) { | ||
try { | ||
this.applicationContext | ||
.publishEvent(new DataSourceInitializedEvent( | ||
initializer.getDataSource())); | ||
// The listener might not be registered yet, so don't rely on it. | ||
if (!this.initialized) { | ||
this.dataSourceInitializer.initSchema(); | ||
this.initialized = true; | ||
} | ||
} | ||
catch (IllegalStateException ex) { | ||
logger.warn("Could not send event to complete DataSource initialization (" | ||
+ ex.getMessage() + ")"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(DataSourceInitializedEvent event) { | ||
// NOTE the event can happen more than once and | ||
// the event datasource is not used here | ||
DataSourceInitializer initializer = getDataSourceInitializer(); | ||
if (!this.initialized && initializer != null) { | ||
initializer.initSchema(); | ||
this.initialized = true; | ||
} | ||
} | ||
|
||
private DataSourceInitializer getDataSourceInitializer() { | ||
if (this.dataSourceInitializer == null) { | ||
DataSource ds = this.dataSource.getIfUnique(); | ||
if (ds != null) { | ||
this.dataSourceInitializer = new DataSourceInitializer(ds, | ||
this.properties, this.applicationContext); | ||
} | ||
} | ||
return this.dataSourceInitializer; | ||
} | ||
|
||
} |
Oops, something went wrong.