Skip to content

Commit be00c1d

Browse files
committed
Fix dependency between cache and hibernate
This commit fixes the ordering between `CacheAutoConfiguration` and `HibernateJpaAutoConfiguration` so that the auto-configured `CacheManager` is configured before Hibernate starts. Closes gh-14181
1 parent 5e7be50 commit be00c1d

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@
311311
<artifactId>hibernate-core</artifactId>
312312
<optional>true</optional>
313313
</dependency>
314+
<dependency>
315+
<groupId>org.hibernate</groupId>
316+
<artifactId>hibernate-ehcache</artifactId>
317+
<optional>true</optional>
318+
</dependency>
314319
<dependency>
315320
<groupId>org.hibernate.validator</groupId>
316321
<artifactId>hibernate-validator</artifactId>

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.beans.factory.InitializingBean;
2222
import org.springframework.beans.factory.ObjectProvider;
2323
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
24-
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2524
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2625
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.CacheConfigurationImportSelector;
2726
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -60,9 +59,8 @@
6059
@ConditionalOnBean(CacheAspectSupport.class)
6160
@ConditionalOnMissingBean(value = CacheManager.class, name = "cacheResolver")
6261
@EnableConfigurationProperties(CacheProperties.class)
63-
@AutoConfigureBefore(HibernateJpaAutoConfiguration.class)
6462
@AutoConfigureAfter({ CouchbaseAutoConfiguration.class, HazelcastAutoConfiguration.class,
65-
RedisAutoConfiguration.class })
63+
HibernateJpaAutoConfiguration.class, RedisAutoConfiguration.class })
6664
@Import(CacheConfigurationImportSelector.class)
6765
public class CacheAutoConfiguration {
6866

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.orm.jpa;
18+
19+
import org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
27+
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
28+
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
29+
import org.springframework.cache.annotation.EnableCaching;
30+
import org.springframework.context.annotation.Configuration;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests for Hibernate 2nd level cache with ehcache2.
36+
*
37+
* @author Stephane Nicoll
38+
*/
39+
@RunWith(ModifiedClassPathRunner.class)
40+
@ClassPathExclusions("ehcache-3*.jar")
41+
public class Hibernate2ndLevelCacheEhCacheIntegrationTests {
42+
43+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
44+
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class,
45+
DataSourceAutoConfiguration.class,
46+
HibernateJpaAutoConfiguration.class))
47+
.withPropertyValues("spring.datasource.initialization-mode=never")
48+
.withUserConfiguration(TestConfiguration.class);
49+
50+
@Test
51+
public void hibernate2ndLevelCacheWithEhCache2() {
52+
this.contextRunner
53+
.withPropertyValues("spring.cache.type=ehcache",
54+
"spring.jpa.properties.hibernate.cache.region.factory_class="
55+
+ SingletonEhCacheRegionFactory.class.getName())
56+
.run((context) -> assertThat(context).hasNotFailed());
57+
}
58+
59+
@Configuration
60+
@EnableCaching
61+
static class TestConfiguration {
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)