Skip to content

Commit 0ad5aa7

Browse files
committed
Enable customization of properties used to create JCache CacheManager
Closes gh-39350
1 parent 5920b27 commit 0ad5aa7

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,21 +38,26 @@
3838
class HazelcastJCacheCustomizationConfiguration {
3939

4040
@Bean
41-
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance) {
42-
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique());
41+
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance,
42+
CacheProperties cacheProperties) {
43+
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique(), cacheProperties);
4344
}
4445

4546
static class HazelcastPropertiesCustomizer implements JCachePropertiesCustomizer {
4647

4748
private final HazelcastInstance hazelcastInstance;
4849

49-
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) {
50+
private final CacheProperties cacheProperties;
51+
52+
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance, CacheProperties cacheProperties) {
5053
this.hazelcastInstance = hazelcastInstance;
54+
this.cacheProperties = cacheProperties;
5155
}
5256

5357
@Override
54-
public void customize(CacheProperties cacheProperties, Properties properties) {
55-
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
58+
public void customize(Properties properties) {
59+
Resource configLocation = this.cacheProperties
60+
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
5661
if (configLocation != null) {
5762
// Hazelcast does not use the URI as a mean to specify a custom config.
5863
properties.setProperty("hazelcast.config.location", toUri(configLocation).toString());

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,7 +95,7 @@ CacheManager jCacheCacheManager(CacheProperties cacheProperties,
9595
private CacheManager createCacheManager(CacheProperties cacheProperties,
9696
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) throws IOException {
9797
CachingProvider cachingProvider = getCachingProvider(cacheProperties.getJcache().getProvider());
98-
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers, cacheProperties);
98+
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers);
9999
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
100100
if (configLocation != null) {
101101
return cachingProvider.getCacheManager(configLocation.getURI(), this.beanClassLoader, properties);
@@ -111,10 +111,9 @@ private CachingProvider getCachingProvider(String cachingProviderFqn) {
111111
}
112112

113113
private Properties createCacheManagerProperties(
114-
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers, CacheProperties cacheProperties) {
114+
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) {
115115
Properties properties = new Properties();
116-
cachePropertiesCustomizers.orderedStream()
117-
.forEach((customizer) -> customizer.customize(cacheProperties, properties));
116+
cachePropertiesCustomizers.orderedStream().forEach((customizer) -> customizer.customize(properties));
118117
return properties;
119118
}
120119

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,15 +26,16 @@
2626
* used by the {@link CachingProvider} to create the {@link CacheManager}.
2727
*
2828
* @author Stephane Nicoll
29+
* @since 3.4.0
30+
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
2931
*/
30-
interface JCachePropertiesCustomizer {
32+
public interface JCachePropertiesCustomizer {
3133

3234
/**
3335
* Customize the properties.
34-
* @param cacheProperties the cache properties
3536
* @param properties the current properties
36-
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
37+
*
3738
*/
38-
void customize(CacheProperties cacheProperties, Properties properties);
39+
void customize(Properties properties);
3940

4041
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.Properties;
2223
import java.util.function.Consumer;
2324

2425
import javax.cache.Caching;
@@ -76,8 +77,10 @@
7677
import org.springframework.test.util.ReflectionTestUtils;
7778

7879
import static org.assertj.core.api.Assertions.assertThat;
80+
import static org.mockito.ArgumentMatchers.any;
7981
import static org.mockito.BDDMockito.given;
8082
import static org.mockito.BDDMockito.then;
83+
import static org.mockito.BDDMockito.willAnswer;
8184
import static org.mockito.Mockito.mock;
8285
import static org.mockito.Mockito.times;
8386

@@ -460,6 +463,23 @@ void jCacheCacheUseBeanClassLoader() {
460463
});
461464
}
462465

466+
@Test
467+
void jCacheCacheWithPropertiesCustomizer() {
468+
JCachePropertiesCustomizer customizer = mock(JCachePropertiesCustomizer.class);
469+
willAnswer((invocation) -> {
470+
invocation.getArgument(0, Properties.class).setProperty("customized", "true");
471+
return null;
472+
}).given(customizer).customize(any(Properties.class));
473+
String cachingProviderFqn = MockCachingProvider.class.getName();
474+
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
475+
.withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn)
476+
.withBean(JCachePropertiesCustomizer.class, () -> customizer)
477+
.run((context) -> {
478+
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
479+
assertThat(cacheManager.getCacheManager().getProperties()).containsEntry("customized", "true");
480+
});
481+
}
482+
463483
@Test
464484
void hazelcastCacheExplicit() {
465485
this.contextRunner.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))

0 commit comments

Comments
 (0)