-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, DataSource initialization was triggered via a BeanPostProcessor or a schema created event from JPA. This caused numerous problems with circular dependencies, bean lifecycle, etc and added significant complexity. This commit reworks DataSource initialization to remove the use of a BeanPostProcessor entirely. In its place, DataSource initialization is now driven by an InitializingBean with dependency relationships between beans ensuring that initialization has been performed before the DataSource is used. This aligns with the approach that's worked well with Flyway and Liquibase. More changes are planned to further simplify DataSource initialization. The changes in this commit are a foundation for those changes. Any new public API in this commit is highly likely to change before the next GA. Fixes gh-13042 Fixes gh-23736
- Loading branch information
1 parent
b3d73a1
commit 2f83a67
Showing
19 changed files
with
539 additions
and
538 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...e/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitialization.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-2021 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 | ||
* | ||
* https://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.springframework.beans.factory.InitializingBean; | ||
import org.springframework.context.ResourceLoaderAware; | ||
import org.springframework.core.io.ResourceLoader; | ||
|
||
/** | ||
* {@link InitializingBean} that performs {@link DataSource} initialization using DDL and | ||
* DML scripts. | ||
* | ||
* @author Andy Wilkinson | ||
* @since 2.5.0 | ||
*/ | ||
public class DataSourceInitialization implements InitializingBean, ResourceLoaderAware { | ||
|
||
private final DataSource dataSource; | ||
|
||
private final DataSourceProperties properies; | ||
|
||
private volatile ResourceLoader resourceLoader; | ||
|
||
/** | ||
* Creates a new {@link DataSourceInitialization} that will initialize the given | ||
* {@code DataSource} using the settings from the given {@code properties}. | ||
* @param dataSource the DataSource to initialize | ||
* @param properies the properties containing the initialization settings | ||
*/ | ||
public DataSourceInitialization(DataSource dataSource, DataSourceProperties properies) { | ||
this.dataSource = dataSource; | ||
this.properies = properies; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
new DataSourceInitializer(this.dataSource, this.properies, this.resourceLoader).initializeDataSource(); | ||
} | ||
|
||
@Override | ||
public void setResourceLoader(ResourceLoader resourceLoader) { | ||
this.resourceLoader = resourceLoader; | ||
} | ||
|
||
} |
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
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: 0 additions & 106 deletions
106
...c/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.