pull/4399/head
Phillip Webb 9 years ago
parent ce73dec992
commit e06198e9cb

@ -33,15 +33,16 @@ import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
/** /**
* Additional configuration to ensure that {@link EntityManagerFactory} beans * Additional configuration to ensure that {@link EntityManagerFactory} beans depend-on
* depend-on the {@code hazelcastInstance} bean. * the {@code hazelcastInstance} bean.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.3.0
*/ */
@Configuration @Configuration
@ConditionalOnClass({ HazelcastInstance.class, LocalContainerEntityManagerFactoryBean.class }) @ConditionalOnClass({ HazelcastInstance.class,
@AutoConfigureAfter({ HazelcastAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) LocalContainerEntityManagerFactoryBean.class })
@AutoConfigureAfter({ HazelcastAutoConfiguration.class,
HibernateJpaAutoConfiguration.class })
class HazelcastJpaDependencyAutoConfiguration { class HazelcastJpaDependencyAutoConfiguration {
@Bean @Bean
@ -56,6 +57,7 @@ class HazelcastJpaDependencyAutoConfiguration {
HazelcastInstanceJpaDependencyPostProcessor() { HazelcastInstanceJpaDependencyPostProcessor() {
super("hazelcastInstance"); super("hazelcastInstance");
} }
} }
static class OnHazelcastAndJpaCondition extends AllNestedConditions { static class OnHazelcastAndJpaCondition extends AllNestedConditions {

@ -382,8 +382,8 @@ public class CacheAutoConfigurationTests {
public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException { public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException {
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, "spring.cache.type=hazelcast", EnvironmentTestUtils.addEnvironment(applicationContext,
"spring.hazelcast.config=" + mainConfig); "spring.cache.type=hazelcast", "spring.hazelcast.config=" + mainConfig);
applicationContext.register(DefaultCacheConfiguration.class); applicationContext.register(DefaultCacheConfiguration.class);
applicationContext.register(HazelcastAndCacheConfiguration.class); applicationContext.register(HazelcastAndCacheConfiguration.class);
applicationContext.refresh(); applicationContext.refresh();
@ -404,14 +404,14 @@ public class CacheAutoConfigurationTests {
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, "spring.cache.type=hazelcast", EnvironmentTestUtils.addEnvironment(applicationContext,
"spring.cache.type=hazelcast",
"spring.cache.hazelcast.config=" + cacheConfig, "spring.cache.hazelcast.config=" + cacheConfig,
"spring.hazelcast.config=" + mainConfig); "spring.hazelcast.config=" + mainConfig);
applicationContext.register(DefaultCacheConfiguration.class); applicationContext.register(DefaultCacheConfiguration.class);
applicationContext.register(HazelcastAndCacheConfiguration.class); applicationContext.register(HazelcastAndCacheConfiguration.class);
applicationContext.refresh(); applicationContext.refresh();
this.context = applicationContext; this.context = applicationContext;
HazelcastInstance hazelcastInstance = this.context HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class); .getBean(HazelcastInstance.class);
HazelcastCacheManager cacheManager = validateCacheManager( HazelcastCacheManager cacheManager = validateCacheManager(
@ -681,7 +681,8 @@ public class CacheAutoConfigurationTests {
} }
@Configuration @Configuration
@ImportAutoConfiguration({CacheAutoConfiguration.class, HazelcastAutoConfiguration.class}) @ImportAutoConfiguration({ CacheAutoConfiguration.class,
HazelcastAutoConfiguration.class })
static class HazelcastAndCacheConfiguration { static class HazelcastAndCacheConfiguration {
} }

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.hazelcast;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import org.junit.After; import org.junit.After;
@ -57,7 +58,7 @@ public class HazelcastJpaDependencyAutoConfigurationTests {
@Test @Test
public void registrationIfHazelcastInstanceHasRegularBeanName() { public void registrationIfHazelcastInstanceHasRegularBeanName() {
load(HazelcastConfiguration.class); load(HazelcastConfiguration.class);
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class), assertThat(getPostProcessor(),
hasKey("hazelcastInstanceJpaDependencyPostProcessor")); hasKey("hazelcastInstanceJpaDependencyPostProcessor"));
assertThat(getEntityManagerFactoryDependencies(), hasItem("hazelcastInstance")); assertThat(getEntityManagerFactoryDependencies(), hasItem("hazelcastInstance"));
} }
@ -65,16 +66,18 @@ public class HazelcastJpaDependencyAutoConfigurationTests {
@Test @Test
public void noRegistrationIfHazelcastInstanceHasCustomBeanName() { public void noRegistrationIfHazelcastInstanceHasCustomBeanName() {
load(HazelcastCustomNameConfiguration.class); load(HazelcastCustomNameConfiguration.class);
assertThat(getEntityManagerFactoryDependencies(), not(hasItem("hazelcastInstance"))); assertThat(getEntityManagerFactoryDependencies(),
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class), not(hasItem("hazelcastInstance")));
assertThat(getPostProcessor(),
not(hasKey("hazelcastInstanceJpaDependencyPostProcessor"))); not(hasKey("hazelcastInstanceJpaDependencyPostProcessor")));
} }
@Test @Test
public void noRegistrationWithNoHazelcastInstance() { public void noRegistrationWithNoHazelcastInstance() {
load(null); load(null);
assertThat(getEntityManagerFactoryDependencies(), not(hasItem("hazelcastInstance"))); assertThat(getEntityManagerFactoryDependencies(),
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class), not(hasItem("hazelcastInstance")));
assertThat(getPostProcessor(),
not(hasKey("hazelcastInstanceJpaDependencyPostProcessor"))); not(hasKey("hazelcastInstanceJpaDependencyPostProcessor")));
} }
@ -84,13 +87,20 @@ public class HazelcastJpaDependencyAutoConfigurationTests {
this.context.register(HazelcastConfiguration.class, this.context.register(HazelcastConfiguration.class,
HazelcastJpaDependencyAutoConfiguration.class); HazelcastJpaDependencyAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class), assertThat(getPostProcessor(),
not(hasKey("hazelcastInstanceJpaDependencyPostProcessor"))); not(hasKey("hazelcastInstanceJpaDependencyPostProcessor")));
} }
private Map<String, EntityManagerFactoryDependsOnPostProcessor> getPostProcessor() {
return this.context
.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class);
}
private List<String> getEntityManagerFactoryDependencies() { private List<String> getEntityManagerFactoryDependencies() {
String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory").getDependsOn(); String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory")
return dependsOn != null ? Arrays.asList(dependsOn) : Collections.<String>emptyList(); .getDependsOn();
return dependsOn != null ? Arrays.asList(dependsOn)
: Collections.<String>emptyList();
} }
public void load(Class<?> config) { public void load(Class<?> config) {

Loading…
Cancel
Save