类org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource源码实例Demo

下面列出了怎么用org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource的API类实例代码及写法,或者点击链接到github查看源代码。

@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
		assertThatIllegalStateException().isThrownBy(() ->
				service.cacheWithException("test", false));
	}
	finally {
		context.close();
	}
}
 
@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();
	}
}
 
源代码3 项目: lams   文件: AbstractJCacheConfiguration.java
@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;
}
 
@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;
}
 
@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();
	}
}
 
@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());
}
 
@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();
}
 
@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 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();
}
 
@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());
}
 
@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();
}
 
@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 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();
}
 
@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());
}
 
@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();
}
 
@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 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();
}
 
@Bean(name = "jCacheOperationSource")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public JCacheOperationSource cacheOperationSource() {
	return new DefaultJCacheOperationSource(
			this.cacheManager, this.cacheResolver, this.exceptionCacheResolver, this.keyGenerator);
}
 
@Bean(name = "jCacheOperationSource")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public JCacheOperationSource cacheOperationSource() {
	return new DefaultJCacheOperationSource(
			this.cacheManager, this.cacheResolver, this.exceptionCacheResolver, this.keyGenerator);
}
 
 类方法
 同包方法