Commit 2c4f88e2 authored by Stephane Nicoll's avatar Stephane Nicoll

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 721b5a23 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
parent 157c0b6c
......@@ -32,6 +32,7 @@ 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.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
......@@ -64,7 +65,7 @@ import org.springframework.util.Assert;
@ConditionalOnMissingBean({ CacheManager.class, CacheResolver.class })
@EnableConfigurationProperties(CacheProperties.class)
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
@AutoConfigureAfter(RedisAutoConfiguration.class)
@AutoConfigureAfter({ HazelcastAutoConfiguration.class, RedisAutoConfiguration.class })
@Import(CacheConfigurationImportSelector.class)
public class CacheAutoConfiguration {
......
......@@ -24,7 +24,6 @@ import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
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.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
......@@ -53,7 +52,6 @@ import org.springframework.core.io.Resource;
@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class })
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
@AutoConfigureAfter(HazelcastAutoConfiguration.class)
class HazelcastCacheConfiguration {
@Configuration
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment