Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/b855ae97708d4d8c3c1f to your computer and use it in GitHub Desktop.
Save thomasdarimont/b855ae97708d4d8c3c1f to your computer and use it in GitHub Desktop.
package demo;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
@SpringBootApplication
@EnableCaching
public class CacheConfigExample {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(CacheConfigExample.class, args);
BusinessService service = ctx.getBean(BusinessService.class);
System.out.println(service.computeWithCacheA("a"));
System.out.println(service.computeWithCacheB("b"));
System.out.println(service.computeWithCacheA("a"));
System.out.println(service.computeWithCacheB("b"));
}
@Bean
public CacheManager cacheManagerA() {
return new ConcurrentMapCacheManager();
}
@Bean
@Primary
public CacheManager cacheManagerB() {
return new ConcurrentMapCacheManager();
}
@Bean
public BusinessService businessService() {
return new DefaultBusinessService();
}
public static interface BusinessService {
BusinessObjectA computeWithCacheA(String param);
BusinessObjectB computeWithCacheB(String param);
}
static class DefaultBusinessService implements BusinessService {
@Override
@Cacheable(value = "businessObjects", cacheManager = "cacheManagerA")
public BusinessObjectA computeWithCacheA(String param) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new BusinessObjectA(param);
}
@Override
@Cacheable(value = "businessObjects", cacheManager = "cacheManagerB")
public BusinessObjectB computeWithCacheB(String param) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new BusinessObjectB(param);
}
}
static class BusinessObject {
String value;
public BusinessObject(String value) {
this.value = value;
}
@Override
public String toString() {
return super.toString() + " value: " + value;
}
}
public static class BusinessObjectA extends BusinessObject {
public BusinessObjectA(String value) {
super(value);
}
}
public static class BusinessObjectB extends BusinessObject {
public BusinessObjectB(String value) {
super(value);
}
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.2.3.RELEASE)
2015-04-08 11:11:04.250 INFO 7110 --- [ main] demo.CacheConfigExample : Starting CacheConfigExample on gauss with PID 7110 (/Users/tom/Documents/dev/ws/sts364-spring/spring-boot-spring-data-redis-cachemanager/target/classes started by tom in /Users/tom/Documents/dev/ws/sts364-spring/spring-boot-spring-data-redis-cachemanager)
2015-04-08 11:11:04.293 INFO 7110 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@51e2adc7: startup date [Wed Apr 08 11:11:04 CEST 2015]; root of context hierarchy
2015-04-08 11:11:04.664 INFO 7110 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'businessService': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=app; factoryMethodName=businessService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [demo/App.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=cacheConfigExample; factoryMethodName=businessService; initMethodName=null; destroyMethodName=(inferred); defined in demo.CacheConfigExample]
2015-04-08 11:11:05.098 INFO 7110 --- [ main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/Users/tom/Documents/dev/ws/sts364-spring/spring-boot-spring-data-redis-cachemanager/target/classes/, file:/Users/tom/.m2/repository/org/springframework/boot/spring-boot-starter-redis/1.2.3.RELEASE/spring-boot-starter-redis-1.2.3.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/boot/spring-boot-starter/1.2.3.RELEASE/spring-boot-starter-1.2.3.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/boot/spring-boot/1.2.3.RELEASE/spring-boot-1.2.3.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.2.3.RELEASE/spring-boot-autoconfigure-1.2.3.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.2.3.RELEASE/spring-boot-starter-logging-1.2.3.RELEASE.jar, file:/Users/tom/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.11/jcl-over-slf4j-1.7.11.jar, file:/Users/tom/.m2/repository/org/slf4j/jul-to-slf4j/1.7.11/jul-to-slf4j-1.7.11.jar, file:/Users/tom/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.11/log4j-over-slf4j-1.7.11.jar, file:/Users/tom/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar, file:/Users/tom/.m2/repository/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar, file:/Users/tom/.m2/repository/org/yaml/snakeyaml/1.14/snakeyaml-1.14.jar, file:/Users/tom/.m2/repository/org/springframework/spring-context-support/4.1.6.RELEASE/spring-context-support-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/spring-beans/4.1.6.RELEASE/spring-beans-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/spring-context/4.1.6.RELEASE/spring-context-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/spring-expression/4.1.6.RELEASE/spring-expression-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/spring-core/4.1.6.RELEASE/spring-core-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/spring-tx/4.1.6.RELEASE/spring-tx-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/data/spring-data-redis/1.4.2.RELEASE/spring-data-redis-1.4.2.RELEASE.jar, file:/Users/tom/.m2/repository/org/springframework/spring-aop/4.1.6.RELEASE/spring-aop-4.1.6.RELEASE.jar, file:/Users/tom/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/Users/tom/.m2/repository/org/slf4j/slf4j-api/1.7.11/slf4j-api-1.7.11.jar, file:/Users/tom/.m2/repository/redis/clients/jedis/2.5.2/jedis-2.5.2.jar, file:/Users/tom/.m2/repository/org/apache/commons/commons-pool2/2.2/commons-pool2-2.2.jar]
2015-04-08 11:11:05.100 INFO 7110 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)
2015-04-08 11:11:05.104 ERROR 7110 --- [ main] o.s.boot.SpringApplication : Application startup failed
java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary or declare a specific CacheManager to use.
at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:185)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.CacheConfigExample.main(CacheConfigExample.java:24)
2015-04-08 11:11:05.104 INFO 7110 --- [ main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@51e2adc7: startup date [Wed Apr 08 11:11:04 CEST 2015]; root of context hierarchy
2015-04-08 11:11:05.105 WARN 7110 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@51e2adc7: startup date [Wed Apr 08 11:11:04 CEST 2015]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:357)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.CacheConfigExample.main(CacheConfigExample.java:24)
2015-04-08 11:11:05.106 INFO 7110 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Exception in thread "main" java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary or declare a specific CacheManager to use.
at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:185)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.CacheConfigExample.main(CacheConfigExample.java:24)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment