30
30
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
31
31
import org .springframework .boot .autoconfigure .condition .ConditionOutcome ;
32
32
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
33
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnSingleCandidate ;
33
34
import org .springframework .boot .autoconfigure .condition .SpringBootCondition ;
34
35
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
35
36
import org .springframework .boot .autoconfigure .orm .jpa .HibernateJpaAutoConfiguration .HibernateEntityManagerCondition ;
37
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
36
38
import org .springframework .boot .orm .jpa .hibernate .SpringJtaPlatform ;
37
39
import org .springframework .context .annotation .ConditionContext ;
38
40
import org .springframework .context .annotation .Conditional ;
55
57
* @author Josh Long
56
58
* @author Manuel Doninger
57
59
* @author Andy Wilkinson
60
+ * @author Kazuki Shimizu
58
61
*/
59
62
@ Configuration
60
63
@ ConditionalOnClass ({ LocalContainerEntityManagerFactoryBean .class ,
61
64
EnableTransactionManagement .class , EntityManager .class })
62
65
@ Conditional (HibernateEntityManagerCondition .class )
63
66
@ AutoConfigureAfter ({ DataSourceAutoConfiguration .class })
64
- public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
67
+ @ EnableConfigurationProperties (JpaProperties .class )
68
+ public class HibernateJpaAutoConfiguration {
65
69
66
70
private static final Log logger = LogFactory
67
71
.getLog (HibernateJpaAutoConfiguration .class );
@@ -83,110 +87,117 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
83
87
"org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform" ,
84
88
"org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" , };
85
89
86
- public HibernateJpaAutoConfiguration (DataSource dataSource ,
87
- JpaProperties jpaProperties ,
88
- ObjectProvider <JtaTransactionManager > jtaTransactionManagerProvider ) {
89
- super (dataSource , jpaProperties , jtaTransactionManagerProvider );
90
- }
91
90
92
- @ Override
93
- protected AbstractJpaVendorAdapter createJpaVendorAdapter () {
94
- return new HibernateJpaVendorAdapter ();
95
- }
91
+ @ Configuration
92
+ @ ConditionalOnSingleCandidate (DataSource .class )
93
+ static class HibernateJpaConfiguration extends JpaBaseConfiguration {
96
94
97
- @ Override
98
- protected Map <String , Object > getVendorProperties () {
99
- Map <String , Object > vendorProperties = new LinkedHashMap <String , Object >();
100
- vendorProperties .putAll (getProperties ().getHibernateProperties (getDataSource ()));
101
- return vendorProperties ;
102
- }
95
+ HibernateJpaConfiguration (DataSource dataSource ,
96
+ JpaProperties jpaProperties ,
97
+ ObjectProvider <JtaTransactionManager > jtaTransactionManagerProvider ) {
98
+ super (dataSource , jpaProperties , jtaTransactionManagerProvider );
99
+ }
100
+
101
+ @ Override
102
+ protected AbstractJpaVendorAdapter createJpaVendorAdapter () {
103
+ return new HibernateJpaVendorAdapter ();
104
+ }
103
105
104
- @ Override
105
- protected void customizeVendorProperties (Map <String , Object > vendorProperties ) {
106
- super .customizeVendorProperties (vendorProperties );
107
- if (!vendorProperties .containsKey (JTA_PLATFORM )) {
108
- configureJtaPlatform (vendorProperties );
106
+ @ Override
107
+ protected Map <String , Object > getVendorProperties () {
108
+ Map <String , Object > vendorProperties = new LinkedHashMap <String , Object >();
109
+ vendorProperties .putAll (getProperties ().getHibernateProperties (getDataSource ()));
110
+ return vendorProperties ;
111
+ }
112
+
113
+ @ Override
114
+ protected void customizeVendorProperties (Map <String , Object > vendorProperties ) {
115
+ super .customizeVendorProperties (vendorProperties );
116
+ if (!vendorProperties .containsKey (JTA_PLATFORM )) {
117
+ configureJtaPlatform (vendorProperties );
118
+ }
109
119
}
110
- }
111
120
112
- private void configureJtaPlatform (Map <String , Object > vendorProperties )
113
- throws LinkageError {
114
- JtaTransactionManager jtaTransactionManager = getJtaTransactionManager ();
115
- if (jtaTransactionManager != null ) {
116
- if (runningOnWebSphere ()) {
117
- // We can never use SpringJtaPlatform on WebSphere as
118
- // WebSphereUowTransactionManager has a null TransactionManager
119
- // which will cause Hibernate to NPE
120
- configureWebSphereTransactionPlatform (vendorProperties );
121
+ private void configureJtaPlatform (Map <String , Object > vendorProperties )
122
+ throws LinkageError {
123
+ JtaTransactionManager jtaTransactionManager = getJtaTransactionManager ();
124
+ if (jtaTransactionManager != null ) {
125
+ if (runningOnWebSphere ()) {
126
+ // We can never use SpringJtaPlatform on WebSphere as
127
+ // WebSphereUowTransactionManager has a null TransactionManager
128
+ // which will cause Hibernate to NPE
129
+ configureWebSphereTransactionPlatform (vendorProperties );
130
+ }
131
+ else {
132
+ configureSpringJtaPlatform (vendorProperties , jtaTransactionManager );
133
+ }
121
134
}
122
135
else {
123
- configureSpringJtaPlatform ( vendorProperties , jtaTransactionManager );
136
+ vendorProperties . put ( JTA_PLATFORM , getNoJtaPlatformManager () );
124
137
}
125
138
}
126
- else {
127
- vendorProperties .put (JTA_PLATFORM , getNoJtaPlatformManager ());
128
- }
129
- }
130
139
131
- private boolean runningOnWebSphere () {
132
- return ClassUtils .isPresent (
133
- "com.ibm.websphere.jtaextensions." + "ExtendedJTATransaction" ,
134
- getClass ().getClassLoader ());
135
- }
136
-
137
- private void configureWebSphereTransactionPlatform (
138
- Map <String , Object > vendorProperties ) {
139
- vendorProperties .put (JTA_PLATFORM , getWebSphereJtaPlatformManager ());
140
- }
140
+ private boolean runningOnWebSphere () {
141
+ return ClassUtils .isPresent (
142
+ "com.ibm.websphere.jtaextensions." + "ExtendedJTATransaction" ,
143
+ getClass ().getClassLoader ());
144
+ }
141
145
142
- private Object getWebSphereJtaPlatformManager () {
143
- return getJtaPlatformManager (WEBSPHERE_JTA_PLATFORM_CLASSES );
144
- }
146
+ private void configureWebSphereTransactionPlatform (
147
+ Map <String , Object > vendorProperties ) {
148
+ vendorProperties .put (JTA_PLATFORM , getWebSphereJtaPlatformManager ());
149
+ }
145
150
146
- private void configureSpringJtaPlatform (Map <String , Object > vendorProperties ,
147
- JtaTransactionManager jtaTransactionManager ) {
148
- try {
149
- vendorProperties .put (JTA_PLATFORM ,
150
- new SpringJtaPlatform (jtaTransactionManager ));
151
+ private Object getWebSphereJtaPlatformManager () {
152
+ return getJtaPlatformManager (WEBSPHERE_JTA_PLATFORM_CLASSES );
151
153
}
152
- catch (LinkageError ex ) {
153
- // NoClassDefFoundError can happen if Hibernate 4.2 is used and some
154
- // containers (e.g. JBoss EAP 6) wraps it in the superclass LinkageError
155
- if (!isUsingJndi ()) {
156
- throw new IllegalStateException ("Unable to set Hibernate JTA "
157
- + "platform, are you using the correct "
158
- + "version of Hibernate?" , ex );
154
+
155
+ private void configureSpringJtaPlatform (Map <String , Object > vendorProperties ,
156
+ JtaTransactionManager jtaTransactionManager ) {
157
+ try {
158
+ vendorProperties .put (JTA_PLATFORM ,
159
+ new SpringJtaPlatform (jtaTransactionManager ));
159
160
}
160
- // Assume that Hibernate will use JNDI
161
- if (logger .isDebugEnabled ()) {
162
- logger .debug ("Unable to set Hibernate JTA platform : " + ex .getMessage ());
161
+ catch (LinkageError ex ) {
162
+ // NoClassDefFoundError can happen if Hibernate 4.2 is used and some
163
+ // containers (e.g. JBoss EAP 6) wraps it in the superclass LinkageError
164
+ if (!isUsingJndi ()) {
165
+ throw new IllegalStateException ("Unable to set Hibernate JTA "
166
+ + "platform, are you using the correct "
167
+ + "version of Hibernate?" , ex );
168
+ }
169
+ // Assume that Hibernate will use JNDI
170
+ if (logger .isDebugEnabled ()) {
171
+ logger .debug ("Unable to set Hibernate JTA platform : " + ex .getMessage ());
172
+ }
163
173
}
164
174
}
165
- }
166
175
167
- private boolean isUsingJndi () {
168
- try {
169
- return JndiLocatorDelegate .isDefaultJndiEnvironmentAvailable ();
170
- }
171
- catch (Error ex ) {
172
- return false ;
176
+ private boolean isUsingJndi () {
177
+ try {
178
+ return JndiLocatorDelegate .isDefaultJndiEnvironmentAvailable ();
179
+ }
180
+ catch (Error ex ) {
181
+ return false ;
182
+ }
173
183
}
174
- }
175
184
176
- private Object getNoJtaPlatformManager () {
177
- return getJtaPlatformManager (NO_JTA_PLATFORM_CLASSES );
178
- }
185
+ private Object getNoJtaPlatformManager () {
186
+ return getJtaPlatformManager (NO_JTA_PLATFORM_CLASSES );
187
+ }
179
188
180
- private Object getJtaPlatformManager (String [] candidates ) {
181
- for (String candidate : candidates ) {
182
- try {
183
- return Class .forName (candidate ).newInstance ();
184
- }
185
- catch (Exception ex ) {
186
- // Continue searching
189
+ private Object getJtaPlatformManager (String [] candidates ) {
190
+ for (String candidate : candidates ) {
191
+ try {
192
+ return Class .forName (candidate ).newInstance ();
193
+ }
194
+ catch (Exception ex ) {
195
+ // Continue searching
196
+ }
187
197
}
198
+ throw new IllegalStateException ("Could not configure JTA platform" );
188
199
}
189
- throw new IllegalStateException ( "Could not configure JTA platform" );
200
+
190
201
}
191
202
192
203
@ Order (Ordered .HIGHEST_PRECEDENCE + 20 )
0 commit comments