From 8e0a94f1d708ba614e06b8e52cfb2f7d14ea94de Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 13 Oct 2015 10:54:15 +0200 Subject: [PATCH] Make sure Caching is initialized before JPA support The second level cache of Hibernate can be configured with dedicated factories that look up for the presence of a cache infrastructure. As Hibernate shouldn't have to know about Spring, that lookup is done against the respective proprietary APIs. We now make sure that caching (and the general purpose Hazelcast auto-configuration) is fully processed before JPA kicks in. In particular an explicit `dependsOn` attribute on those beans is added when they are processed. Closes gh-4158 --- .../cache/CacheAutoConfiguration.java | 18 ++++++++++++++++++ .../hazelcast/HazelcastAutoConfiguration.java | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java index 59b41762ba..6b74d7cad1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java @@ -24,12 +24,15 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.CacheConfigurationImportSelector; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; @@ -41,6 +44,8 @@ import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; import org.springframework.context.annotation.Role; import org.springframework.core.type.AnnotationMetadata; +import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.util.Assert; /** @@ -58,6 +63,7 @@ import org.springframework.util.Assert; @ConditionalOnBean(CacheAspectSupport.class) @ConditionalOnMissingBean({ CacheManager.class, CacheResolver.class }) @EnableConfigurationProperties(CacheProperties.class) +@AutoConfigureBefore(HibernateJpaAutoConfiguration.class) @AutoConfigureAfter(RedisAutoConfiguration.class) @Import(CacheConfigurationImportSelector.class) public class CacheAutoConfiguration { @@ -75,6 +81,18 @@ public class CacheAutoConfiguration { return new CacheManagerValidator(); } + @Configuration + @ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) + @ConditionalOnBean(AbstractEntityManagerFactoryBean.class) + protected static class CacheManagerJpaDependencyConfiguration + extends EntityManagerFactoryDependsOnPostProcessor { + + public CacheManagerJpaDependencyConfiguration() { + super("cacheManager"); + } + + } + /** * {@link BeanFactoryPostProcessor} to ensure that the {@link CacheManagerValidator} * is triggered before {@link CacheAspectSupport} but without causing early diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java index aa82898d92..ff2c2115d7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java @@ -20,14 +20,18 @@ import java.io.IOException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; +import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; +import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import com.hazelcast.config.Config; import com.hazelcast.core.Hazelcast; @@ -78,6 +82,18 @@ public class HazelcastAutoConfiguration { } + @Configuration + @ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) + @ConditionalOnBean(AbstractEntityManagerFactoryBean.class) + protected static class HazelcastInstanceJpaDependencyConfiguration + extends EntityManagerFactoryDependsOnPostProcessor { + + public HazelcastInstanceJpaDependencyConfiguration() { + super("hazelcastInstance"); + } + + } + /** * {@link HazelcastConfigResourceCondition} that checks if the * {@code spring.hazelcast.config} configuration key is defined.