Fix Hazelcast auto-configuration ordering

Make sure that the general Hazelcast auto-configuration is processed
before the cache auto-configuration. This was supposed to be fixed and
tested in 721b5a2 but unfortunately the `@AutoConfigureAfter` annotation
was placed on a regular `@Configuration` class (which has no effect).

The tests were passing because the ordering is actually hardcoded in the
test. The relevant tests now use `ImportAutoConfiguration` that simulates
the same order as the one use by the actual application.

Closes gh-4389
pull/3817/merge
Stephane Nicoll 9 years ago
parent 157c0b6cae
commit 2c4f88e221

@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
@ -64,7 +65,7 @@ import org.springframework.util.Assert;
@ConditionalOnMissingBean({ CacheManager.class, CacheResolver.class }) @ConditionalOnMissingBean({ CacheManager.class, CacheResolver.class })
@EnableConfigurationProperties(CacheProperties.class) @EnableConfigurationProperties(CacheProperties.class)
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class) @AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
@AutoConfigureAfter(RedisAutoConfiguration.class) @AutoConfigureAfter({ HazelcastAutoConfiguration.class, RedisAutoConfiguration.class })
@Import(CacheConfigurationImportSelector.class) @Import(CacheConfigurationImportSelector.class)
public class CacheAutoConfiguration { public class CacheAutoConfiguration {

@ -24,7 +24,6 @@ import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager; import com.hazelcast.spring.cache.HazelcastCacheManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
@ -53,7 +52,6 @@ import org.springframework.core.io.Resource;
@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class }) @ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class })
@ConditionalOnMissingBean(CacheManager.class) @ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class) @Conditional(CacheCondition.class)
@AutoConfigureAfter(HazelcastAutoConfiguration.class)
class HazelcastCacheConfiguration { class HazelcastCacheConfiguration {
@Configuration @Configuration

@ -17,7 +17,6 @@
package org.springframework.boot.autoconfigure.cache; package org.springframework.boot.autoconfigure.cache;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -44,6 +43,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider; import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
@ -380,12 +380,14 @@ public class CacheAutoConfigurationTests {
@Test @Test
public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException { public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException {
Collection<Class<?>> configs = new ArrayList<Class<?>>();
configs.add(DefaultCacheConfiguration.class);
configs.add(HazelcastAutoConfiguration.class);
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
doLoad(configs, "spring.cache.type=hazelcast", AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, "spring.cache.type=hazelcast",
"spring.hazelcast.config=" + mainConfig); "spring.hazelcast.config=" + mainConfig);
applicationContext.register(DefaultCacheConfiguration.class);
applicationContext.register(HazelcastAndCacheConfiguration.class);
applicationContext.refresh();
this.context = applicationContext;
HazelcastCacheManager cacheManager = validateCacheManager( HazelcastCacheManager cacheManager = validateCacheManager(
HazelcastCacheManager.class); HazelcastCacheManager.class);
HazelcastInstance hazelcastInstance = this.context HazelcastInstance hazelcastInstance = this.context
@ -399,14 +401,17 @@ public class CacheAutoConfigurationTests {
@Test @Test
public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig() public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig()
throws IOException { throws IOException {
Collection<Class<?>> configs = new ArrayList<Class<?>>();
configs.add(DefaultCacheConfiguration.class);
configs.add(HazelcastAutoConfiguration.class);
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";
doLoad(configs, "spring.cache.type=hazelcast", AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
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(HazelcastAndCacheConfiguration.class);
applicationContext.refresh();
this.context = applicationContext;
HazelcastInstance hazelcastInstance = this.context HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class); .getBean(HazelcastInstance.class);
HazelcastCacheManager cacheManager = validateCacheManager( HazelcastCacheManager cacheManager = validateCacheManager(
@ -557,17 +562,9 @@ public class CacheAutoConfigurationTests {
} }
private void load(Class<?> config, String... environment) { private void load(Class<?> config, String... environment) {
Collection<Class<?>> configs = new ArrayList<Class<?>>();
configs.add(config);
doLoad(configs, environment);
}
private void doLoad(Collection<Class<?>> configs, String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, environment); EnvironmentTestUtils.addEnvironment(applicationContext, environment);
for (Class<?> config : configs) { applicationContext.register(config);
applicationContext.register(config);
}
applicationContext.register(CacheAutoConfiguration.class); applicationContext.register(CacheAutoConfiguration.class);
applicationContext.refresh(); applicationContext.refresh();
this.context = applicationContext; this.context = applicationContext;
@ -683,6 +680,12 @@ public class CacheAutoConfigurationTests {
} }
@Configuration
@ImportAutoConfiguration({CacheAutoConfiguration.class, HazelcastAutoConfiguration.class})
static class HazelcastAndCacheConfiguration {
}
@Configuration @Configuration
@EnableCaching @EnableCaching
static class InfinispanCustomConfiguration { static class InfinispanCustomConfiguration {

Loading…
Cancel
Save