|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2014 the original author or authors. |
| 2 | + * Copyright 2012-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -64,20 +64,33 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
|
64 | 64 | if (bean instanceof JpaProperties) {
|
65 | 65 | this.properties = (JpaProperties) bean;
|
66 | 66 | }
|
67 |
| - if (bean instanceof EntityManagerFactory && this.dataSource != null |
68 |
| - && isInitializingDatabase()) { |
69 |
| - this.applicationContext |
70 |
| - .publishEvent(new DataSourceInitializedEvent(this.dataSource)); |
| 67 | + if (bean instanceof EntityManagerFactory) { |
| 68 | + publishEventIfRequired((EntityManagerFactory) bean); |
71 | 69 | }
|
72 | 70 | return bean;
|
73 | 71 | }
|
74 | 72 |
|
75 |
| - private boolean isInitializingDatabase() { |
| 73 | + private void publishEventIfRequired(EntityManagerFactory entityManagerFactory) { |
| 74 | + DataSource dataSource = findDataSource(entityManagerFactory); |
| 75 | + if (dataSource != null && isInitializingDatabase(dataSource)) { |
| 76 | + this.applicationContext |
| 77 | + .publishEvent(new DataSourceInitializedEvent(dataSource)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private DataSource findDataSource(EntityManagerFactory entityManagerFactory) { |
| 82 | + Object dataSource = entityManagerFactory.getProperties() |
| 83 | + .get("javax.persistence.nonJtaDataSource"); |
| 84 | + return (dataSource != null && dataSource instanceof DataSource |
| 85 | + ? (DataSource) dataSource : this.dataSource); |
| 86 | + } |
| 87 | + |
| 88 | + private boolean isInitializingDatabase(DataSource dataSource) { |
76 | 89 | if (this.properties == null) {
|
77 | 90 | return true; // better safe than sorry
|
78 | 91 | }
|
79 | 92 | Map<String, String> hibernate = this.properties
|
80 |
| - .getHibernateProperties(this.dataSource); |
| 93 | + .getHibernateProperties(dataSource); |
81 | 94 | if (hibernate.containsKey("hibernate.hbm2ddl.auto")) {
|
82 | 95 | return true;
|
83 | 96 | }
|
|
0 commit comments