mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-06-30 00:36:49 +08:00
Enable customization of properties used to create JCache CacheManager
Closes gh-39350
This commit is contained in:
parent
5920b27b57
commit
0ad5aa7400
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -38,21 +38,26 @@ import org.springframework.core.io.Resource;
|
|||
class HazelcastJCacheCustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance) {
|
||||
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique());
|
||||
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance,
|
||||
CacheProperties cacheProperties) {
|
||||
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique(), cacheProperties);
|
||||
}
|
||||
|
||||
static class HazelcastPropertiesCustomizer implements JCachePropertiesCustomizer {
|
||||
|
||||
private final HazelcastInstance hazelcastInstance;
|
||||
|
||||
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) {
|
||||
private final CacheProperties cacheProperties;
|
||||
|
||||
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance, CacheProperties cacheProperties) {
|
||||
this.hazelcastInstance = hazelcastInstance;
|
||||
this.cacheProperties = cacheProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(CacheProperties cacheProperties, Properties properties) {
|
||||
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
|
||||
public void customize(Properties properties) {
|
||||
Resource configLocation = this.cacheProperties
|
||||
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
|
||||
if (configLocation != null) {
|
||||
// Hazelcast does not use the URI as a mean to specify a custom config.
|
||||
properties.setProperty("hazelcast.config.location", toUri(configLocation).toString());
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -95,7 +95,7 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware {
|
|||
private CacheManager createCacheManager(CacheProperties cacheProperties,
|
||||
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) throws IOException {
|
||||
CachingProvider cachingProvider = getCachingProvider(cacheProperties.getJcache().getProvider());
|
||||
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers, cacheProperties);
|
||||
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers);
|
||||
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
|
||||
if (configLocation != null) {
|
||||
return cachingProvider.getCacheManager(configLocation.getURI(), this.beanClassLoader, properties);
|
||||
|
@ -111,10 +111,9 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware {
|
|||
}
|
||||
|
||||
private Properties createCacheManagerProperties(
|
||||
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers, CacheProperties cacheProperties) {
|
||||
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) {
|
||||
Properties properties = new Properties();
|
||||
cachePropertiesCustomizers.orderedStream()
|
||||
.forEach((customizer) -> customizer.customize(cacheProperties, properties));
|
||||
cachePropertiesCustomizers.orderedStream().forEach((customizer) -> customizer.customize(properties));
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -26,15 +26,16 @@ import javax.cache.spi.CachingProvider;
|
|||
* used by the {@link CachingProvider} to create the {@link CacheManager}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 3.4.0
|
||||
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
|
||||
*/
|
||||
interface JCachePropertiesCustomizer {
|
||||
public interface JCachePropertiesCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the properties.
|
||||
* @param cacheProperties the cache properties
|
||||
* @param properties the current properties
|
||||
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
|
||||
*
|
||||
*/
|
||||
void customize(CacheProperties cacheProperties, Properties properties);
|
||||
void customize(Properties properties);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.cache;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.cache.Caching;
|
||||
|
@ -76,8 +77,10 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
|
@ -460,6 +463,23 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void jCacheCacheWithPropertiesCustomizer() {
|
||||
JCachePropertiesCustomizer customizer = mock(JCachePropertiesCustomizer.class);
|
||||
willAnswer((invocation) -> {
|
||||
invocation.getArgument(0, Properties.class).setProperty("customized", "true");
|
||||
return null;
|
||||
}).given(customizer).customize(any(Properties.class));
|
||||
String cachingProviderFqn = MockCachingProvider.class.getName();
|
||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||
.withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn)
|
||||
.withBean(JCachePropertiesCustomizer.class, () -> customizer)
|
||||
.run((context) -> {
|
||||
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheManager().getProperties()).containsEntry("customized", "true");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void hazelcastCacheExplicit() {
|
||||
this.contextRunner.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
|
||||
|
|
Loading…
Reference in New Issue
Block a user