Guice

Guice: differences between Singleton.class and @Singleton

北慕城南 提交于 2019-12-14 03:40:24
问题 In Guice, what's the difference between: // Inside your AbstractModule subclass: @Override public void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); } And: @Override public void configure() { bind(Service.class).to(ServiceImpl.class); } @Provides @Singleton public ServiceImpl providesService() { return new ServiceImpl(); } Are they both the same? When would you use one versus the other? Thanks in advance. 回答1: They are nearly identical. The @Singleton syntax is

Guice DAO Provider in thread pool - queries become 'idle in transation'

蓝咒 提交于 2019-12-14 02:07:23
问题 I use Java 8, Hibernate 5.1.0.Final and Guice 4.1.0. @Inject private Provider<ExampleDAO> exampleDAOProvider; public void test(){ ExecutorService threadPool = Executors.newFixedThreadPool(10); for (int i = 0; i < 100; i++) threadPool.execute(new Runnable() { @Override public void run() { logger.info(exampleDAOProvider.find(1l)); } }); threadPool.shutdown(); } Every test() method execution will produce 10 (thread pool size) rows more in pg_stat_activity . They are simple select * from queries

How to indicate one or more match in url pattern?

帅比萌擦擦* 提交于 2019-12-13 22:18:33
问题 "/*" will match zero or more characters. I want to serve request if there is a one or more characters proceeding the "/" character. Note: I am using Guice's Servlet Module to configure the request. Thanks! 回答1: I think you can just do this, assuming you mean that you want to matching anything with a / and 1 or more characters after it (so /foo and /a but not / ). serveRegex("/.+") 回答2: in general regex: /+ dont know if it works here tough ..? http://code.google.com/p/google-guice/wiki

Intercept a constructor with google guice

依然范特西╮ 提交于 2019-12-13 19:39:29
问题 I want to intercept a constructor with Google guice. I have added my annotation mark on TYPE but I have some problem with the "bindInterceptor" If I write bindInterceptor(Matchers.annotatedWith(ReactToLoad.class), Matchers.any(), new ReactToLoadInterceptor()); This will run the interceptor on each method. (it's bad and it's normal) bindInterceptor( Matchers.any(), Matchers.annotatedWith(ReactToLoad.class), new ReactToLoadInterceptor()); If I run the code just below, I MUST put the annotation

How to bind Assisted Injected class to interface?

岁酱吖の 提交于 2019-12-13 19:34:57
问题 Here is the problem I met: Class SimpleCommand implements Executable{ private final ConfigManager config; private String name; @Inject public SimpleCommand(ConfigManager config, @Assisted String name){ this.config = config; this.name = name; } } Class MyModule extends AbstractModule{ @Override protected void configure() { bind(CommandFactory.class).toProvider(FactoryProvider.newFactory(CommandFactory.class, SimpleCommand.class)); bind(Executable.class).to(SimpleCommand.class); } } When I try

Error in binding: No implementation was bound - Guice

ε祈祈猫儿з 提交于 2019-12-13 18:17:45
问题 I’m trying to use Guice to solve dependency. I’ve read through tutorials and examples, but I still can’t figure out why I keep getting this error: No implementation for com.edit.owl.persistence.PersistentStore<com.edit.common.domain.Foo> annotated with @com.google.inject.name.Named(value=FooPersistence) was bound. while locating com.edit.owl.persistence.PersistentStore<com.edit.common.domain.Foo> annotated with @com.google.inject.name.Named(value=FooPersistence) for parameter 0 at com.edit

Guice post execution method interception

拥有回忆 提交于 2019-12-13 15:46:50
问题 In Guice, is there a way for my MethodInterceptor::invoke implementation to be invoked after the intercepted method is executed (and not immediately before)? I've added the current code to my AbstractModule : bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor()); 回答1: To execute code after the method invocation in an interceptor (this applies not just to Guice), you have to use a try/finally combination:

Is it possible to inject the class requesting injection using Guice?

纵饮孤独 提交于 2019-12-13 15:26:40
问题 I'd like an injected instance of an object to know the name of the class that is requesting its injection. I'm aware that this kind of violates the entire concept of dependency injection, but it seems like a valid use case for supporting useful logging. Is this possible with Guice? Example: class InjectorAware { @Inject public InjectorAware(Class injectorClass){ System.out.println("I was injected into a "+injectorClass.getCanonicalName()); } } class NeedsInjectorAwareField { @Inject

Error creating BlobContext using jclouds in a Spring MVC application

倾然丶 夕夏残阳落幕 提交于 2019-12-13 14:41:50
问题 I have a Spring MVC 4.0.1 web application that needs to upload files to Rackspace Cloud Files. I am using Apache jClouds in order to do this. When trying to create the BlobStore using the following code: BlobStoreContext context = ContextBuilder.newBuilder("cloudfiles-us").credentials("username","password").buildView(BlobStoreContext.class); I get the following exception: com.google.inject.CreationException: Guice creation errors: 1) No implementation for com.google.common.base.Supplier<java

How do you prevent Guice from injecting a class not bound in the Module?

我的未来我决定 提交于 2019-12-13 13:13:22
问题 import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceDemo { public static void main(String[] args) { new GuiceDemo().run(); } public void run() { Injector injector = Guice.createInjector(new EmptyModule()); DemoInstance demoInstance = injector.getInstance(DemoInstance.class); assert(demoInstance.demoUnbound == null); } public static class EmptyModule extends AbstractModule { @Override