diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java index 0a714869c10..74810e47a2e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * 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. @@ -64,20 +64,33 @@ class DataSourceInitializedPublisher implements BeanPostProcessor { if (bean instanceof JpaProperties) { this.properties = (JpaProperties) bean; } - if (bean instanceof EntityManagerFactory && this.dataSource != null - && isInitializingDatabase()) { - this.applicationContext - .publishEvent(new DataSourceInitializedEvent(this.dataSource)); + if (bean instanceof EntityManagerFactory) { + publishEventIfRequired((EntityManagerFactory) bean); } return bean; } - private boolean isInitializingDatabase() { + private void publishEventIfRequired(EntityManagerFactory entityManagerFactory) { + DataSource dataSource = findDataSource(entityManagerFactory); + if (dataSource != null && isInitializingDatabase(dataSource)) { + this.applicationContext + .publishEvent(new DataSourceInitializedEvent(dataSource)); + } + } + + private DataSource findDataSource(EntityManagerFactory entityManagerFactory) { + Object dataSource = entityManagerFactory.getProperties() + .get("javax.persistence.nonJtaDataSource"); + return (dataSource != null && dataSource instanceof DataSource + ? (DataSource) dataSource : this.dataSource); + } + + private boolean isInitializingDatabase(DataSource dataSource) { if (this.properties == null) { return true; // better safe than sorry } Map hibernate = this.properties - .getHibernateProperties(this.dataSource); + .getHibernateProperties(dataSource); if (hibernate.containsKey("hibernate.hbm2ddl.auto")) { return true; }