Commit 15cbef3d authored by Andy Wilkinson's avatar Andy Wilkinson

Update JpaBaseConfiguration to use constructor injection

This change was missed as part of the work done in 19d8c5e6.

See gh-5306
Closes gh-5543
parent e41ccfae
...@@ -25,6 +25,7 @@ import javax.sql.DataSource; ...@@ -25,6 +25,7 @@ import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
...@@ -53,6 +54,7 @@ import org.springframework.util.ClassUtils; ...@@ -53,6 +54,7 @@ import org.springframework.util.ClassUtils;
* @author Phillip Webb * @author Phillip Webb
* @author Josh Long * @author Josh Long
* @author Manuel Doninger * @author Manuel Doninger
* @author Andy Wilkinson
*/ */
@Configuration @Configuration
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, @ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
...@@ -81,14 +83,10 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { ...@@ -81,14 +83,10 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
"org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform", "org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform",
"org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform", }; "org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform", };
private final JpaProperties properties; public HibernateJpaAutoConfiguration(DataSource dataSource,
JpaProperties jpaProperties,
private final DataSource dataSource; ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
super(dataSource, jpaProperties, jtaTransactionManagerProvider);
public HibernateJpaAutoConfiguration(JpaProperties properties,
DataSource dataSource) {
this.properties = properties;
this.dataSource = dataSource;
} }
@Override @Override
...@@ -99,7 +97,7 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { ...@@ -99,7 +97,7 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
@Override @Override
protected Map<String, Object> getVendorProperties() { protected Map<String, Object> getVendorProperties() {
Map<String, Object> vendorProperties = new LinkedHashMap<String, Object>(); Map<String, Object> vendorProperties = new LinkedHashMap<String, Object>();
vendorProperties.putAll(this.properties.getHibernateProperties(this.dataSource)); vendorProperties.putAll(getProperties().getHibernateProperties(getDataSource()));
return vendorProperties; return vendorProperties;
} }
......
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -25,7 +25,7 @@ import javax.sql.DataSource; ...@@ -25,7 +25,7 @@ import javax.sql.DataSource;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
...@@ -57,6 +57,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter ...@@ -57,6 +57,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer * @author Dave Syer
* @author Oliver Gierke * @author Oliver Gierke
* @author Andy Wilkinson
*/ */
@EnableConfigurationProperties(JpaProperties.class) @EnableConfigurationProperties(JpaProperties.class)
@Import(DataSourceInitializedPublisher.Registrar.class) @Import(DataSourceInitializedPublisher.Registrar.class)
...@@ -64,19 +65,20 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { ...@@ -64,19 +65,20 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
private static final String[] NO_PACKAGES = new String[0]; private static final String[] NO_PACKAGES = new String[0];
private ConfigurableListableBeanFactory beanFactory; private final DataSource dataSource;
@Autowired private final JpaProperties properties;
private DataSource dataSource;
@Autowired(required = false) private final JtaTransactionManager jtaTransactionManager;
private PersistenceUnitManager persistenceUnitManager;
@Autowired private ConfigurableListableBeanFactory beanFactory;
private JpaProperties jpaProperties;
@Autowired(required = false) protected JpaBaseConfiguration(DataSource dataSource, JpaProperties properties,
private JtaTransactionManager jtaTransactionManager; ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
this.dataSource = dataSource;
this.properties = properties;
this.jtaTransactionManager = jtaTransactionManagerProvider.getIfAvailable();
}
@Bean @Bean
@ConditionalOnMissingBean(PlatformTransactionManager.class) @ConditionalOnMissingBean(PlatformTransactionManager.class)
...@@ -88,20 +90,21 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { ...@@ -88,20 +90,21 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public JpaVendorAdapter jpaVendorAdapter() { public JpaVendorAdapter jpaVendorAdapter() {
AbstractJpaVendorAdapter adapter = createJpaVendorAdapter(); AbstractJpaVendorAdapter adapter = createJpaVendorAdapter();
adapter.setShowSql(this.jpaProperties.isShowSql()); adapter.setShowSql(this.properties.isShowSql());
adapter.setDatabase(this.jpaProperties.getDatabase()); adapter.setDatabase(this.properties.getDatabase());
adapter.setDatabasePlatform(this.jpaProperties.getDatabasePlatform()); adapter.setDatabasePlatform(this.properties.getDatabasePlatform());
adapter.setGenerateDdl(this.jpaProperties.isGenerateDdl()); adapter.setGenerateDdl(this.properties.isGenerateDdl());
return adapter; return adapter;
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder( public EntityManagerFactoryBuilder entityManagerFactoryBuilder(
JpaVendorAdapter jpaVendorAdapter) { JpaVendorAdapter jpaVendorAdapter,
ObjectProvider<PersistenceUnitManager> persistenceUnitManagerProvider) {
EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder( EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder(
jpaVendorAdapter, this.jpaProperties.getProperties(), jpaVendorAdapter, this.properties.getProperties(),
this.persistenceUnitManager); persistenceUnitManagerProvider.getIfAvailable());
builder.setCallback(getVendorCallback()); builder.setCallback(getVendorCallback());
return builder; return builder;
} }
...@@ -158,6 +161,22 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { ...@@ -158,6 +161,22 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
return (this.jtaTransactionManager != null); return (this.jtaTransactionManager != null);
} }
/**
* Return the {@link JpaProperties}.
* @return the properties
*/
protected final JpaProperties getProperties() {
return this.properties;
}
/**
* Return the {@link DataSource}.
* @return the data source
*/
protected final DataSource getDataSource() {
return this.dataSource;
}
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment