diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/Bucket4jStartupCheckConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/Bucket4jStartupCheckConfiguration.java new file mode 100644 index 00000000..d0d85a4a --- /dev/null +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/Bucket4jStartupCheckConfiguration.java @@ -0,0 +1,39 @@ +package com.giffing.bucket4j.spring.boot.starter; + +import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; +import com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration; +import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; +import jakarta.annotation.Nullable; +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; +import org.springframework.context.annotation.Configuration; + + +@Configuration +@AutoConfigureAfter( value = { + CacheAutoConfiguration.class, + Bucket4jCacheConfiguration.class +}) +@RequiredArgsConstructor +public class Bucket4jStartupCheckConfiguration { + + + @Nullable + private final SyncCacheResolver syncCacheResolver; + + @Nullable + private final AsyncCacheResolver asyncCacheResolver; + + + @PostConstruct + public void checkCache() { + // TODO check filter configuration, check method configuration + if(syncCacheResolver == null && asyncCacheResolver == null) { + // TODO throw proper exception + throw new IllegalStateException("cache not configured properly"); + } + } + +} diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/aspect/Bucket4jAopConfig.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/aspect/Bucket4jAopConfig.java index e8cc8b2a..3c0ac084 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/aspect/Bucket4jAopConfig.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/aspect/Bucket4jAopConfig.java @@ -2,6 +2,7 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnBucket4jEnabled; import com.giffing.bucket4j.spring.boot.starter.config.metrics.actuator.SpringBootActuatorConfig; import com.giffing.bucket4j.spring.boot.starter.config.service.ServiceConfiguration; import com.giffing.bucket4j.spring.boot.starter.context.RateLimiting; @@ -12,7 +13,6 @@ import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -22,8 +22,8 @@ * Enables the support for the {@link RateLimiting} annotation to rate limit on method level. */ @Configuration +@ConditionalOnBucket4jEnabled @ConditionalOnClass(Aspect.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = {"enabled"}, matchIfMissing = true) @EnableConfigurationProperties({Bucket4JBootProperties.class}) @AutoConfigureAfter(value = { CacheAutoConfiguration.class, Bucket4jCacheConfiguration.class }) @ConditionalOnBean(value = SyncCacheResolver.class) diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastReactiveBucket4jCacheConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastReactiveBucket4jCacheConfiguration.java index 9e535479..4395441c 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastReactiveBucket4jCacheConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastReactiveBucket4jCacheConfiguration.java @@ -3,6 +3,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheBucket4jConfiguration; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import com.hazelcast.core.HazelcastInstance; @@ -21,7 +23,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @ConditionalOnClass({HazelcastInstance.class}) @ConditionalOnBean(HazelcastInstance.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "hazelcast-reactive", matchIfMissing = true) +@ConditionalOnCache("hazelcast-reactive") public class HazelcastReactiveBucket4jCacheConfiguration { private final HazelcastInstance hazelcastInstance; @@ -41,14 +43,14 @@ public AsyncCacheResolver hazelcastCacheResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { IMap map = hazelcastInstance.getMap(configCacheName); return new HazelcastCacheManager<>(map); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public HazelcastCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { IMap map = hazelcastInstance.getMap(configCacheName); return new HazelcastCacheListener<>(map, eventPublisher); diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastSpringBucket4jCacheConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastSpringBucket4jCacheConfiguration.java index e69fa703..90c75a94 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastSpringBucket4jCacheConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/hazelcast/HazelcastSpringBucket4jCacheConfiguration.java @@ -4,6 +4,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; import com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheBucket4jConfiguration; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import com.hazelcast.core.HazelcastInstance; @@ -23,7 +25,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass({HazelcastCacheManager.class}) @ConditionalOnBean(HazelcastCacheManager.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "hazelcast-spring", matchIfMissing = true) +@ConditionalOnCache("hazelcast-spring") public class HazelcastSpringBucket4jCacheConfiguration { private final HazelcastInstance hazelcastInstance; @@ -42,14 +44,14 @@ public AsyncCacheResolver hazelcastCacheResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { IMap map = hazelcastInstance.getMap(configCacheName); return new com.giffing.bucket4j.spring.boot.starter.config.cache.hazelcast.HazelcastCacheManager<>(map); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public HazelcastCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { IMap map = hazelcastInstance.getMap(configCacheName); return new HazelcastCacheListener<>(map, eventPublisher); diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/ignite/IgniteBucket4jCacheConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/ignite/IgniteBucket4jCacheConfiguration.java index d06c965c..99362c67 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/ignite/IgniteBucket4jCacheConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/ignite/IgniteBucket4jCacheConfiguration.java @@ -2,6 +2,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import org.apache.ignite.Ignite; @@ -14,7 +16,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @ConditionalOnClass({ Ignite.class }) @ConditionalOnBean(Ignite.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "ignite", matchIfMissing = true) +@ConditionalOnCache("ignite") public class IgniteBucket4jCacheConfiguration { private final Ignite ignite; @@ -33,13 +35,13 @@ public AsyncCacheResolver hazelcastCacheResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { return new IgniteCacheManager<>(ignite.cache(configCacheName)); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public IgniteCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { return new IgniteCacheListener<>(ignite.cache(configCacheName), eventPublisher); } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/infinispan/InfinispanBucket4jCacheConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/infinispan/InfinispanBucket4jCacheConfiguration.java index 295299ee..85792775 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/infinispan/InfinispanBucket4jCacheConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/infinispan/InfinispanBucket4jCacheConfiguration.java @@ -2,6 +2,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import org.infinispan.manager.CacheContainer; @@ -15,7 +17,7 @@ @ConditionalOnClass({CacheContainer.class}) @ConditionalOnBean(CacheContainer.class) @ConditionalOnMissingBean(AsyncCacheResolver.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "infinispan", matchIfMissing = true) +@ConditionalOnCache("infinispan") public class InfinispanBucket4jCacheConfiguration { private final CacheContainer cacheContainer; @@ -33,13 +35,13 @@ public AsyncCacheResolver infinispanCacheResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { return new InfinispanCacheManager<>(cacheContainer.getCache(configCacheName)); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public InfinispanCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName), eventPublisher); } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/InfinispanJCacheBucket4jConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/InfinispanJCacheBucket4jConfiguration.java index 932249ba..0813a57a 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/InfinispanJCacheBucket4jConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/InfinispanJCacheBucket4jConfiguration.java @@ -5,6 +5,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheListener; import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheResolver; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import org.infinispan.manager.CacheContainer; @@ -24,7 +26,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass({CacheContainer.class, Caching.class, JCacheCacheManager.class}) @ConditionalOnBean(CacheContainer.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "jcache-ignite", matchIfMissing = true) +@ConditionalOnCache("jcache-ignite") public class InfinispanJCacheBucket4jConfiguration { private final CacheContainer cacheContainer; @@ -43,13 +45,13 @@ public SyncCacheResolver bucket4jCacheResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { return new InfinispanCacheManager<>(cacheContainer.getCache(configCacheName)); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public InfinispanCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName), eventPublisher); } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/JCacheBucket4jConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/JCacheBucket4jConfiguration.java index 52289974..86f42fd5 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/JCacheBucket4jConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/jcache/JCacheBucket4jConfiguration.java @@ -1,6 +1,8 @@ package com.giffing.bucket4j.spring.boot.starter.config.cache.jcache; import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import org.springframework.boot.autoconfigure.condition.*; @@ -16,35 +18,35 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass({Caching.class, JCacheCacheManager.class}) @ConditionalOnBean(CacheManager.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "jcache", matchIfMissing = true) +@ConditionalOnCache("jcache") public class JCacheBucket4jConfiguration { - private final CacheManager cacheManager; - private final String configCacheName; - - public JCacheBucket4jConfiguration(CacheManager cacheManager, Bucket4JBootProperties properties) { - this.cacheManager = cacheManager; - this.configCacheName = properties.getFilterConfigCacheName(); - } - - @Bean - @ConditionalOnMissingBean(SyncCacheResolver.class) - public SyncCacheResolver bucket4jCacheResolver() { - return new JCacheCacheResolver(cacheManager); - } - - @Bean - @ConditionalOnMissingBean(com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") - public com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager configCacheManager( - ) { - return new com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheCacheManager<>(cacheManager.getCache(configCacheName)); - } - - @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") - public JCacheCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { - return new JCacheCacheListener<>(cacheManager.getCache(configCacheName), eventPublisher); - } + private final CacheManager cacheManager; + private final String configCacheName; + + public JCacheBucket4jConfiguration(CacheManager cacheManager, Bucket4JBootProperties properties) { + this.cacheManager = cacheManager; + this.configCacheName = properties.getFilterConfigCacheName(); + } + + @Bean + @ConditionalOnMissingBean(SyncCacheResolver.class) + public SyncCacheResolver bucket4jCacheResolver() { + return new JCacheCacheResolver(cacheManager); + } + + @Bean + @ConditionalOnMissingBean(com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager.class) + @ConditionalOnFilterConfigCacheEnabled + public com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager configCacheManager( + ) { + return new com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheCacheManager<>(cacheManager.getCache(configCacheName)); + } + + @Bean + @ConditionalOnFilterConfigCacheEnabled + public JCacheCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { + return new JCacheCacheListener<>(cacheManager.getCache(configCacheName), eventPublisher); + } } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/jedis/JedisBucket4jConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/jedis/JedisBucket4jConfiguration.java index ca7a2377..167da37e 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/jedis/JedisBucket4jConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/jedis/JedisBucket4jConfiguration.java @@ -2,6 +2,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import io.github.bucket4j.redis.jedis.cas.JedisBasedProxyManager.JedisBasedProxyManagerBuilder; @@ -15,7 +17,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass(JedisBasedProxyManagerBuilder.class) @ConditionalOnBean(JedisPool.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "redis-jedis", matchIfMissing = true) +@ConditionalOnCache("redis-jedis") public class JedisBucket4jConfiguration { public final JedisPool jedisPool; @@ -34,13 +36,13 @@ public SyncCacheResolver bucket4RedisResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { return new JedisCacheManager<>(jedisPool, configCacheName, Bucket4JConfiguration.class); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public JedisCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { return new JedisCacheListener<>(jedisPool, configCacheName, String.class, Bucket4JConfiguration.class, eventPublisher); } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/lettuce/LettuceBucket4jConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/lettuce/LettuceBucket4jConfiguration.java index 3395bddb..6840dd9f 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/lettuce/LettuceBucket4jConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/lettuce/LettuceBucket4jConfiguration.java @@ -2,6 +2,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import io.github.bucket4j.redis.lettuce.cas.LettuceBasedProxyManager; @@ -15,7 +17,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @ConditionalOnClass(LettuceBasedProxyManager.class) @ConditionalOnBean(RedisClient.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "redis-lettuce", matchIfMissing = true) +@ConditionalOnCache("redis-lettuce") public class LettuceBucket4jConfiguration { private final RedisClient redisClient; @@ -33,13 +35,13 @@ public AsyncCacheResolver bucket4RedisResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { return new LettuceCacheManager<>(redisClient, configCacheName, Bucket4JConfiguration.class); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public LettuceCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { return new LettuceCacheListener<>(redisClient, configCacheName, String.class, Bucket4JConfiguration.class, eventPublisher); } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/redisson/RedissonBucket4jConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/redisson/RedissonBucket4jConfiguration.java index e0963385..dee2944b 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/redisson/RedissonBucket4jConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/cache/redis/redisson/RedissonBucket4jConfiguration.java @@ -2,6 +2,8 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import io.github.bucket4j.redis.redisson.cas.RedissonBasedProxyManager; @@ -18,7 +20,7 @@ @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @ConditionalOnClass(RedissonBasedProxyManager.class) @ConditionalOnBean(CommandAsyncExecutor.class) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "redis-redisson", matchIfMissing = true) +@ConditionalOnCache("redis-redisson") public class RedissonBucket4jConfiguration { private final CommandAsyncExecutor commandExecutor; @@ -44,13 +46,13 @@ public AsyncCacheResolver bucket4RedisResolver() { @Bean @ConditionalOnMissingBean(CacheManager.class) - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public CacheManager configCacheManager() { return new RedissonCacheManager<>(this.redissonClient, configCacheName); } @Bean - @ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") + @ConditionalOnFilterConfigCacheEnabled public RedissonCacheListener configCacheListener(ApplicationEventPublisher eventPublisher) { return new RedissonCacheListener<>(this.redissonClient, configCacheName, eventPublisher); } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnBucket4jEnabled.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnBucket4jEnabled.java new file mode 100644 index 00000000..071683f5 --- /dev/null +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnBucket4jEnabled.java @@ -0,0 +1,15 @@ +package com.giffing.bucket4j.spring.boot.starter.config.condition; + +import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = { "enabled" }, matchIfMissing = true) +public @interface ConditionalOnBucket4jEnabled { +} diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnCache.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnCache.java new file mode 100644 index 00000000..f1093f1b --- /dev/null +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnCache.java @@ -0,0 +1,19 @@ +package com.giffing.bucket4j.spring.boot.starter.config.condition; + +import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", matchIfMissing = true) +public @interface ConditionalOnCache { + + @AliasFor(annotation = ConditionalOnProperty.class, attribute = "havingValue") + String value() default ""; +} diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnFilterConfigCacheEnabled.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnFilterConfigCacheEnabled.java new file mode 100644 index 00000000..a291fba3 --- /dev/null +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/condition/ConditionalOnFilterConfigCacheEnabled.java @@ -0,0 +1,15 @@ +package com.giffing.bucket4j.spring.boot.starter.config.condition; + +import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true") +public @interface ConditionalOnFilterConfigCacheEnabled { +} diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/gateway/Bucket4JAutoConfigurationSpringCloudGatewayFilter.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/gateway/Bucket4JAutoConfigurationSpringCloudGatewayFilter.java index d3c564cf..6cf9f931 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/gateway/Bucket4JAutoConfigurationSpringCloudGatewayFilter.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/gateway/Bucket4JAutoConfigurationSpringCloudGatewayFilter.java @@ -4,6 +4,7 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheUpdateEvent; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnBucket4jEnabled; import com.giffing.bucket4j.spring.boot.starter.config.filter.Bucket4JBaseConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.filter.reactive.predicate.WebfluxExecutePredicateConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.metrics.actuator.SpringBootActuatorConfig; @@ -23,7 +24,6 @@ import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.gateway.config.GatewayAutoConfiguration; import org.springframework.cloud.gateway.filter.GlobalFilter; @@ -43,9 +43,9 @@ * Configures Servlet Filters for Bucket4Js rate limit. */ @Configuration +@ConditionalOnBucket4jEnabled @ConditionalOnClass({ GlobalFilter.class }) @EnableConfigurationProperties({ Bucket4JBootProperties.class }) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = { "enabled" }, matchIfMissing = true) @AutoConfigureBefore(GatewayAutoConfiguration.class) @AutoConfigureAfter(value = { CacheAutoConfiguration.class, Bucket4jCacheConfiguration.class }) @ConditionalOnBean(value = AsyncCacheResolver.class) diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/webflux/Bucket4JAutoConfigurationWebfluxFilter.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/webflux/Bucket4JAutoConfigurationWebfluxFilter.java index 1e25c121..97185075 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/webflux/Bucket4JAutoConfigurationWebfluxFilter.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/reactive/webflux/Bucket4JAutoConfigurationWebfluxFilter.java @@ -4,6 +4,7 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheUpdateEvent; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnBucket4jEnabled; import com.giffing.bucket4j.spring.boot.starter.config.filter.Bucket4JBaseConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.filter.reactive.predicate.WebfluxExecutePredicateConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.metrics.actuator.SpringBootActuatorConfig; @@ -24,7 +25,6 @@ import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @@ -44,9 +44,9 @@ * Configures Servlet Filters for Bucket4Js rate limit. */ @Configuration +@ConditionalOnBucket4jEnabled @ConditionalOnClass({ WebFilter.class }) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = { "enabled" }, matchIfMissing = true) -@AutoConfigureBefore(value = {WebFluxAutoConfiguration.class}) +@AutoConfigureBefore(value = { WebFluxAutoConfiguration.class }) @AutoConfigureAfter(value = { CacheAutoConfiguration.class, Bucket4jCacheConfiguration.class }) @ConditionalOnBean(value = AsyncCacheResolver.class) @EnableConfigurationProperties({ Bucket4JBootProperties.class}) diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/servlet/Bucket4JAutoConfigurationServletFilter.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/servlet/Bucket4JAutoConfigurationServletFilter.java index f688b6cf..351dc3c6 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/servlet/Bucket4JAutoConfigurationServletFilter.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/filter/servlet/Bucket4JAutoConfigurationServletFilter.java @@ -4,10 +4,10 @@ import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager; import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheUpdateEvent; import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnBucket4jEnabled; import com.giffing.bucket4j.spring.boot.starter.config.filter.Bucket4JBaseConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.filter.servlet.predicate.ServletRequestExecutePredicateConfiguration; import com.giffing.bucket4j.spring.boot.starter.config.metrics.actuator.SpringBootActuatorConfig; -import com.giffing.bucket4j.spring.boot.starter.service.RateLimitService; import com.giffing.bucket4j.spring.boot.starter.config.service.ServiceConfiguration; import com.giffing.bucket4j.spring.boot.starter.context.Bucket4jConfigurationHolder; import com.giffing.bucket4j.spring.boot.starter.context.ExecutePredicate; @@ -16,6 +16,7 @@ import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration; import com.giffing.bucket4j.spring.boot.starter.filter.servlet.ServletRequestFilter; +import com.giffing.bucket4j.spring.boot.starter.service.RateLimitService; import jakarta.servlet.Filter; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -26,7 +27,6 @@ import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.server.WebServerFactoryCustomizer; @@ -45,8 +45,8 @@ * Configures {@link Filter}s for Bucket4Js rate limit. */ @Configuration +@ConditionalOnBucket4jEnabled @ConditionalOnClass({ Filter.class }) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = {"enabled"}, matchIfMissing = true) @EnableConfigurationProperties({ Bucket4JBootProperties.class }) @AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class) @AutoConfigureAfter(value = { CacheAutoConfiguration.class, Bucket4jCacheConfiguration.class }) diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricsConfiguration.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricsConfiguration.java index 95ebed32..97d29ebb 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricsConfiguration.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricsConfiguration.java @@ -1,21 +1,18 @@ package com.giffing.bucket4j.spring.boot.starter.config.metrics.actuator; +import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnBucket4jEnabled; +import com.giffing.bucket4j.spring.boot.starter.context.metrics.MetricHandler; +import io.micrometer.core.instrument.Metrics; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import com.giffing.bucket4j.spring.boot.starter.context.metrics.MetricHandler; -import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties; - -import io.micrometer.core.instrument.Metrics; - @Configuration @ConditionalOnClass(value = {Metrics.class}) -@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX + ".metrics", value = { "enabled" }, matchIfMissing = true) +@ConditionalOnBucket4jEnabled public class Bucket4jMetricsConfiguration { - + @Bean @Primary public MetricHandler springBoot2Bucket4jMetricHandler() { diff --git a/bucket4j-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/bucket4j-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 498c0d0d..725b84e9 100644 --- a/bucket4j-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/bucket4j-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,5 +1,6 @@ +com.giffing.bucket4j.spring.boot.starter.Bucket4jStartupCheckConfiguration com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration com.giffing.bucket4j.spring.boot.starter.config.aspect.Bucket4jAopConfig com.giffing.bucket4j.spring.boot.starter.config.filter.reactive.gateway.Bucket4JAutoConfigurationSpringCloudGatewayFilter com.giffing.bucket4j.spring.boot.starter.config.filter.servlet.Bucket4JAutoConfigurationServletFilter -com.giffing.bucket4j.spring.boot.starter.config.filter.reactive.webflux.Bucket4JAutoConfigurationWebfluxFilter +com.giffing.bucket4j.spring.boot.starter.config.filter.reactive.webflux.Bucket4JAutoConfigurationWebfluxFilter \ No newline at end of file diff --git a/examples/gateway/src/test/java/com/giffing/bucket4j/spring/boot/starter/examples/gateway/GatewaySampleApplicationTest.java b/examples/gateway/src/test/java/com/giffing/bucket4j/spring/boot/starter/examples/gateway/GatewaySampleApplicationTest.java index 810a23b0..97b65974 100644 --- a/examples/gateway/src/test/java/com/giffing/bucket4j/spring/boot/starter/examples/gateway/GatewaySampleApplicationTest.java +++ b/examples/gateway/src/test/java/com/giffing/bucket4j/spring/boot/starter/examples/gateway/GatewaySampleApplicationTest.java @@ -31,7 +31,10 @@ "httpbin=http://localhost:${wiremock.server.port}", }) @AutoConfigureWireMock(port = 0) -@TestPropertySource(properties = {"bucket4j.filter-config-caching-enabled=true", "bucket4j.filter-config-cache-name=filterConfigCache"}) +@TestPropertySource(properties = { + "bucket4j.filter-config-caching-enabled=true", + "bucket4j.filter-config-cache-name=filterConfigCache" +}) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class GatewaySampleApplicationTest { @@ -66,9 +69,7 @@ void helloTest() throws Exception { IntStream.rangeClosed(1, 5) .boxed() .sorted(Collections.reverseOrder()) - .forEach(counter -> { - successfulWebRequest(url, counter - 1); - }); + .forEach(counter -> successfulWebRequest(url, counter - 1)); blockedWebRequestDueToRateLimit(url); } diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/MethodTestSuite.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/MethodTestSuite.java index 3a2c4ede..0f657ab7 100644 --- a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/MethodTestSuite.java +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/MethodTestSuite.java @@ -5,7 +5,8 @@ @Suite @SelectClasses({ - MethodRateLimitTest.class + MethodRateLimitTest.class, + NoCacheFoundTest.class }) public class MethodTestSuite { } diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/NoCacheFoundTest.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/NoCacheFoundTest.java new file mode 100644 index 00000000..7fdb93c8 --- /dev/null +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/method/method/NoCacheFoundTest.java @@ -0,0 +1,36 @@ +package com.giffing.bucket4j.spring.boot.starter.general.tests.method.method; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.boot.SpringApplication; +import org.springframework.test.annotation.DirtiesContext; + +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@DirtiesContext +public class NoCacheFoundTest { + + @Test + public void test() { + SpringApplication springApplication = new SpringApplication(MethodTestApplication.class); + Properties properties = new Properties(); + properties.put("bucket4j.cache-to-use", "does_not_exist"); + properties.put("bucket4j.methods[0].name", "default"); + properties.put("bucket4j.methods[0].cache-name", "buckets"); + properties.put("bucket4j.methods[0].rate-limit.bandwidths[0].capacity", "5"); + properties.put("bucket4j.methods[0].rate-limit.bandwidths[0].time", "10"); + properties.put("bucket4j.methods[0].rate-limit.bandwidths[0].unit", "seconds"); + properties.put("bucket4j.methods[0].rate-limit.bandwidths[0].refill-speed", "greedy"); + springApplication.setDefaultProperties(properties); + + var beanCreationException = Assertions.assertThrows(BeanCreationException.class, springApplication::run); + assertNotNull(beanCreationException); + Throwable rootCause = beanCreationException.getRootCause(); + assertNotNull(rootCause); + assertEquals("cache not configured properly", rootCause.getMessage()); + } +}