protected JCacheOperationSource createOperationSource(CacheManager cacheManager, CacheResolver cacheResolver, CacheResolver exceptionCacheResolver, KeyGenerator keyGenerator) { DefaultJCacheOperationSource source = new DefaultJCacheOperationSource(); source.setCacheManager(cacheManager); source.setCacheResolver(cacheResolver); source.setExceptionCacheResolver(exceptionCacheResolver); source.setKeyGenerator(keyGenerator); source.setBeanFactory(new StaticListableBeanFactory()); source.afterSingletonsInstantiated(); return source; }
@Override protected CacheResolver getDefaultCacheResolver() { if (getCacheResolver() == null) { this.cacheResolver = SingletonSupplier.of(new SimpleCacheResolver(getDefaultCacheManager())); } return getCacheResolver(); }
@Override public void afterSingletonsInstantiated() { // Make sure that the cache resolver is initialized. An exception cache resolver is only // required if the exceptionCacheName attribute is set on an operation. Assert.notNull(getDefaultCacheResolver(), "Cache resolver should have been initialized"); }
@Test public void bothSetOnlyResolverIsUsed() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FullCachingConfigSupport.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); assertSame(context.getBean("cacheResolver"), cos.getCacheResolver()); assertSame(context.getBean("keyGenerator"), cos.getKeyGenerator()); assertSame(context.getBean("exceptionCacheResolver"), cos.getExceptionCacheResolver()); context.close(); }
@Test public void emptyConfigSupport() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); assertNotNull(cos.getCacheResolver()); assertEquals(SimpleCacheResolver.class, cos.getCacheResolver().getClass()); assertSame(context.getBean(CacheManager.class), ((SimpleCacheResolver) cos.getCacheResolver()).getCacheManager()); assertNull(cos.getExceptionCacheResolver()); context.close(); }
@Bean(name = "jCacheOperationSource") @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public JCacheOperationSource cacheOperationSource() { return new DefaultJCacheOperationSource( this.cacheManager, this.cacheResolver, this.exceptionCacheResolver, this.keyGenerator); }
@Override protected CacheResolver getDefaultExceptionCacheResolver() { if (getExceptionCacheResolver() == null) { this.exceptionCacheResolver = SingletonSupplier.of(new LazyCacheResolver()); } return getExceptionCacheResolver(); }
@Test public void cacheResolver() { ConfigurableApplicationContext context = new GenericXmlApplicationContext( "/org/springframework/cache/jcache/config/jCacheNamespaceDriven-resolver.xml"); DefaultJCacheOperationSource ci = context.getBean(DefaultJCacheOperationSource.class); assertSame(context.getBean("cacheResolver"), ci.getCacheResolver()); context.close(); }
@Override protected CacheResolver getDefaultCacheResolver() { if (this.cacheResolver == null) { this.cacheResolver = new SimpleCacheResolver(getDefaultCacheManager()); } return this.cacheResolver; }
protected CacheManager getDefaultCacheManager() { if (getCacheManager() == null) { Assert.state(this.beanFactory != null, "BeanFactory required for default CacheManager resolution"); try { this.cacheManager = SingletonSupplier.of(this.beanFactory.getBean(CacheManager.class)); } catch (NoUniqueBeanDefinitionException ex) { throw new IllegalStateException("No unique bean of type CacheManager found. "+ "Mark one as primary or declare a specific CacheManager to use."); } catch (NoSuchBeanDefinitionException ex) { throw new IllegalStateException("No bean of type CacheManager found. Register a CacheManager "+ "bean or remove the @EnableCaching annotation from your configuration."); } } return getCacheManager(); }
@Test public void fullCachingConfig() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(FullCachingConfig.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator()); assertSame(context.getBean("cacheResolver", CacheResolver.class), cos.getCacheResolver()); assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class), cos.getExceptionCacheResolver()); JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class); assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler()); }
@Bean(name = "jCacheOperationSource") @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public JCacheOperationSource cacheOperationSource() { return new DefaultJCacheOperationSource( this.cacheManager, this.cacheResolver, this.exceptionCacheResolver, this.keyGenerator); }
@Override protected CacheResolver getDefaultExceptionCacheResolver() { if (getExceptionCacheResolver() == null) { this.exceptionCacheResolver = SingletonSupplier.of(new LazyCacheResolver()); } return getExceptionCacheResolver(); }
@Test public void exceptionCacheResolverLazilyRequired() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(NoExceptionCacheResolverConfig.class); try { DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); assertSame(context.getBean("cacheResolver"), cos.getCacheResolver()); JCacheableService<?> service = context.getBean(JCacheableService.class); service.cache("id"); // This call requires the cache manager to be set thrown.expect(IllegalStateException.class); service.cacheWithException("test", false); } finally { context.close(); } }
@Override public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) { if (this.cacheResolver == null) { this.cacheResolver = new SimpleExceptionCacheResolver(getDefaultCacheManager()); } return this.cacheResolver.resolveCaches(context); } }
protected CacheManager getDefaultCacheManager() { if (getCacheManager() == null) { Assert.state(this.beanFactory != null, "BeanFactory required for default CacheManager resolution"); try { this.cacheManager = SingletonSupplier.of(this.beanFactory.getBean(CacheManager.class)); } catch (NoUniqueBeanDefinitionException ex) { throw new IllegalStateException("No unique bean of type CacheManager found. "+ "Mark one as primary or declare a specific CacheManager to use."); } catch (NoSuchBeanDefinitionException ex) { throw new IllegalStateException("No bean of type CacheManager found. Register a CacheManager "+ "bean or remove the @EnableCaching annotation from your configuration."); } } return getCacheManager(); }
@Override protected CacheResolver getDefaultCacheResolver() { if (getCacheResolver() == null) { this.cacheResolver = SingletonSupplier.of(new SimpleCacheResolver(getDefaultCacheManager())); } return getCacheResolver(); }
@Bean(name = "jCacheOperationSource") @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public JCacheOperationSource cacheOperationSource() { DefaultJCacheOperationSource source = new DefaultJCacheOperationSource(); if (this.cacheManager != null) { source.setCacheManager(this.cacheManager); } if (this.keyGenerator != null) { source.setKeyGenerator(this.keyGenerator); } if (this.cacheResolver != null) { source.setCacheResolver(this.cacheResolver); } if (this.exceptionCacheResolver != null) { source.setExceptionCacheResolver(this.exceptionCacheResolver); } return source; }
@Override public void afterSingletonsInstantiated() { // Make sure that the cache resolver is initialized. An exception cache resolver is only // required if the exceptionCacheName attribute is set on an operation. Assert.notNull(getDefaultCacheResolver(), "Cache resolver should have been initialized"); }
@Override public void afterSingletonsInstantiated() { // Make sure that the cache resolver is initialized. An exception cache resolver is only // required if the exceptionCacheName attribute is set on an operation Assert.notNull(getDefaultCacheResolver(), "Cache resolver should have been initialized"); }