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
This commit is contained in:
Stephane Nicoll
2015-11-06 17:02:12 +01:00
parent 157c0b6cae
commit 2c4f88e221
3 changed files with 23 additions and 21 deletions

View File

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