From 15cbef3df0e5cacf4c168993aa561290a35bdc7f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 4 Apr 2016 18:55:31 +0100 Subject: [PATCH] Update JpaBaseConfiguration to use constructor injection This change was missed as part of the work done in 19d8c5e6. See gh-5306 Closes gh-5543 --- .../jpa/HibernateJpaAutoConfiguration.java | 16 +++-- .../orm/jpa/JpaBaseConfiguration.java | 59 ++++++++++++------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java index 10a1700583..6abaee1842 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java @@ -25,6 +25,7 @@ import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; @@ -53,6 +54,7 @@ import org.springframework.util.ClassUtils; * @author Phillip Webb * @author Josh Long * @author Manuel Doninger + * @author Andy Wilkinson */ @Configuration @ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, @@ -81,14 +83,10 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { "org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform", "org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform", }; - private final JpaProperties properties; - - private final DataSource dataSource; - - public HibernateJpaAutoConfiguration(JpaProperties properties, - DataSource dataSource) { - this.properties = properties; - this.dataSource = dataSource; + public HibernateJpaAutoConfiguration(DataSource dataSource, + JpaProperties jpaProperties, + ObjectProvider jtaTransactionManagerProvider) { + super(dataSource, jpaProperties, jtaTransactionManagerProvider); } @Override @@ -99,7 +97,7 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { @Override protected Map getVendorProperties() { Map vendorProperties = new LinkedHashMap(); - vendorProperties.putAll(this.properties.getHibernateProperties(this.dataSource)); + vendorProperties.putAll(getProperties().getHibernateProperties(getDataSource())); return vendorProperties; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java index da8b82f97d..6763138702 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java @@ -1,5 +1,5 @@ /* - * 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"); * you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ import javax.sql.DataSource; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; 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.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -57,6 +57,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter * @author Phillip Webb * @author Dave Syer * @author Oliver Gierke + * @author Andy Wilkinson */ @EnableConfigurationProperties(JpaProperties.class) @Import(DataSourceInitializedPublisher.Registrar.class) @@ -64,19 +65,20 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { private static final String[] NO_PACKAGES = new String[0]; + private final DataSource dataSource; + + private final JpaProperties properties; + + private final JtaTransactionManager jtaTransactionManager; + private ConfigurableListableBeanFactory beanFactory; - @Autowired - private DataSource dataSource; - - @Autowired(required = false) - private PersistenceUnitManager persistenceUnitManager; - - @Autowired - private JpaProperties jpaProperties; - - @Autowired(required = false) - private JtaTransactionManager jtaTransactionManager; + protected JpaBaseConfiguration(DataSource dataSource, JpaProperties properties, + ObjectProvider jtaTransactionManagerProvider) { + this.dataSource = dataSource; + this.properties = properties; + this.jtaTransactionManager = jtaTransactionManagerProvider.getIfAvailable(); + } @Bean @ConditionalOnMissingBean(PlatformTransactionManager.class) @@ -88,20 +90,21 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { @ConditionalOnMissingBean public JpaVendorAdapter jpaVendorAdapter() { AbstractJpaVendorAdapter adapter = createJpaVendorAdapter(); - adapter.setShowSql(this.jpaProperties.isShowSql()); - adapter.setDatabase(this.jpaProperties.getDatabase()); - adapter.setDatabasePlatform(this.jpaProperties.getDatabasePlatform()); - adapter.setGenerateDdl(this.jpaProperties.isGenerateDdl()); + adapter.setShowSql(this.properties.isShowSql()); + adapter.setDatabase(this.properties.getDatabase()); + adapter.setDatabasePlatform(this.properties.getDatabasePlatform()); + adapter.setGenerateDdl(this.properties.isGenerateDdl()); return adapter; } @Bean @ConditionalOnMissingBean public EntityManagerFactoryBuilder entityManagerFactoryBuilder( - JpaVendorAdapter jpaVendorAdapter) { + JpaVendorAdapter jpaVendorAdapter, + ObjectProvider persistenceUnitManagerProvider) { EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilder( - jpaVendorAdapter, this.jpaProperties.getProperties(), - this.persistenceUnitManager); + jpaVendorAdapter, this.properties.getProperties(), + persistenceUnitManagerProvider.getIfAvailable()); builder.setCallback(getVendorCallback()); return builder; } @@ -158,6 +161,22 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { 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 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;