Skip to content

Commit f1ecf53

Browse files
author
Dave Syer
committed
Delay instantiation of DataSource as late as possible
Unfortunately it still has to happen in a @PostConstruct (otherwise JPA never sees the schema in time), but we can delay a bit by not using @Autowired. Appears to fix the Spring Cloud problem (spring-cloud/spring-cloud-config#105). Fixes gh-2658
1 parent 018310e commit f1ecf53

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
5050
@Autowired
5151
private ConfigurableApplicationContext applicationContext;
5252

53-
@Autowired(required = false)
5453
private DataSource dataSource;
5554

5655
@Autowired
@@ -59,11 +58,14 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
5958
private boolean initialized = false;
6059

6160
@PostConstruct
62-
protected void initialize() {
61+
public void init() {
6362
if (!this.properties.isInitialize()) {
6463
logger.debug("Initialization disabled (not running DDL scripts)");
6564
return;
6665
}
66+
if (applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) {
67+
this.dataSource = applicationContext.getBean(DataSource.class);
68+
}
6769
if (this.dataSource == null) {
6870
logger.debug("No DataSource found so not initializing");
6971
return;

0 commit comments

Comments
 (0)