jooby-project / jooby

The modular web framework for Java and Kotlin
https://jooby.io
Apache License 2.0
1.71k stars 197 forks source link

Inject Issue in Cache modules and MVC #573

Closed Shbnv closed 8 years ago

Shbnv commented 8 years ago

There is an issue with inject any Cache modules into MVC Controller. For example, Guava: { use(GuavaCache.newCache()); session(GuavaSessionStore.class); } @Inject public MyMVCRoutes( Cache cache) { this.cache = cache; } Getting error: `[2016-11-23 14:09:48,483]-[HotSwap] ERROR App - An error occurred while stating the application: com.google.inject.CreationException: Unable to create injector, see the following errors:

1) No implementation for com.google.common.cache.Cache was bound. while locating com.google.common.cache.Cache for the 1st parameter of MyMVCRoutes.(MyMVCRoutes.java:58

at org.jooby.Jooby.lambda$bindService$50(Jooby.java:2757)

1 error at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:470) at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorreator.java:155) at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:10) at com.google.inject.Guice.createInjector(Guice.java:99) at com.google.inject.Guice.createInjector(Guice.java:84) at org.jooby.Jooby.bootstrap(Jooby.java:2498) at org.jooby.Jooby.start(Jooby.java:1931) at org.jooby.Jooby.start(Jooby.java:1919) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.jooby.run.Main.lambda$startApp$1(Main.java:237) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)`

jknack commented 8 years ago

Hi @Shbnv

Try Cache<String, Object>, like:

@Inject public MyMVCRoutes( Cache<String, Object> cache) { this.cache = cache; }
Shbnv commented 8 years ago

I tried this already: 1) No implementation for com.google.common.cache.Cache<java.lang.String, java.lang.Object> was bound. while locating com.google.common.cache.Cache<java.lang.String, java.lang.Object>

jknack commented 8 years ago

Found it!

Did you create a guava.session entry in .conf file for GuavaSessionStore, right?

That overrides the default guava cache and results in the error you are seeing now. Try this:

guava.cache = "maximumSize=10"
guava.session = "maximumSize=10"

The guava.cache is the default cache (the one created by GuavaCache.newCache()).

Guess we can probably improve this... not sure.

Shbnv commented 8 years ago

Yes! Thank you!