Guice

Is there a way to check if I'm inside a servlet request with Guice?

回眸只為那壹抹淺笑 提交于 2020-01-05 03:32:14
问题 I'm writing a JUL logging Handler and I'd like to augment the logged messages with information about the current request, if we're currently handling a request. To accomplish this, I've injected a Provider<Thing> into the Handler , where Thing is @RequestScoped . However, calling provider.get() throws an OutOfScopeException if the logging happens while we're not handling a request. I feel like catching the OutOfScopeException would be bad form. Is there a better way to determine whether or

Using the provider from two different scopes

允我心安 提交于 2020-01-04 18:17:33
问题 I have the following problem with Guice: a singleton service, is injected with provider of context-sensitive information. Until now, context was related only to servlet requests, so I used a @RequestScoped provider, and I was injecting this provider in service like so: @RequestScoped public class ContextProvider<IContext> implements Provider<IContext> { @Override public IContext get() { ... } // returns context } @Singleton public class ServiceImpl implements IService { @Inject private

Using the provider from two different scopes

一曲冷凌霜 提交于 2020-01-04 18:17:26
问题 I have the following problem with Guice: a singleton service, is injected with provider of context-sensitive information. Until now, context was related only to servlet requests, so I used a @RequestScoped provider, and I was injecting this provider in service like so: @RequestScoped public class ContextProvider<IContext> implements Provider<IContext> { @Override public IContext get() { ... } // returns context } @Singleton public class ServiceImpl implements IService { @Inject private

what's the difference between these two binding declarations with Google Guice?

岁酱吖の 提交于 2020-01-04 09:54:39
问题 What's the difference between bind(FooImpl.class).in(Scopes.SINGLETON); bind(Foo.class).to(FooImpl.class); and bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON); With Google Guice? edit: The second declaration create two instances on Singleton in a project I am working on. Reference here 回答1: In reference to Google Guice documentation: In linked bindings, scopes apply to the binding source, not the binding target. Suppose we have a class Applebees that implements both Bar and Grill

guice assisted inject + multibinding + generics

末鹿安然 提交于 2020-01-04 09:23:13
问题 I'm trying to combine this 3 features of Guice: inject, multibinding, generics. I create a prototype of production project, so here it is: First, this is a little hierarchy for generics(in production case there is hierarchy of N entities): public interface Type { } public class Type1 implements Type{ } public class Type2 implements Type { } Next, classes ToCreate1 and ToCreate2 I want to create by Factory . Base class: public abstract class AbstractToCreate<T extends Type> { public T type;

Generic Bind with Guice

蹲街弑〆低调 提交于 2020-01-04 07:19:28
问题 I trying to build a simple lib for persistence with guice persist and some other things. I already have a AbstractDao<T> , that I can easily extend and bind the concrete implementation like a boss. But, I want a kind of GenericDao, like this: public abstract class GenericDao<T extends Bean> { @Inject private Provider<EntityManager> emp; protected EntityManager em() { return emp.get(); } public AbstractDao() { } protected abstract Class<T> clazz(); // .... And if I will have just the CRUD

Google guice - multibinding + generics + assistedinject

有些话、适合烂在心里 提交于 2020-01-04 05:15:23
问题 I have the following classes : public interface Factory<T extends MyParentClass> { public T create(String parameter); } public class FactoryImpl1 implements Factory<MyChildClass1> { public MyChildClass1 create(String parameter){ ... } } public class FactoryImpl2 implements Factory<MyChildClass2> { public MyChildClass2 create(String parameter){ ... } } public class MyModule extends AbstractModule { @Override protected void configure() { MapBinder<String, Factory<MyParentClass>> factoryMap =

Configuring cucumber-guice

让人想犯罪 __ 提交于 2020-01-04 03:26:53
问题 I am trying to use DI in my step definitions. I have a module, public class MyModule extends AbstractModule { private final static MyInterface INSTANCE = new MyInterfaceImpl(); @Override protected void configure() { bind(MyInterface.class).toInstance(INSTANCE); } } and want to inject this instance in the constructor of the step definitions. public class MyStepDefs { private final MyInterface instance; @Inject public MyStepDefs(MyInterface instance) { this.instance = instance } } I think I

How to use Play Framework's request and session scope in Google Guice?

梦想的初衷 提交于 2020-01-03 01:49:08
问题 I am using Guice for Dependency Injection in my Play (Java) Framework project, and struggling to understand how the concept of "session" is best used with Guice and Play? I know that Play is stateless and there really is no concept of a session, other than that you can store values in a cookie. My understanding with Guice and Play is that, while Guice documentation describes supporting different scopes (singleton, session, request, no scope), because we are instantiating a new injector with

How to inject a “runtime” dependency like a logged in user which is not available at application boot time?

大兔子大兔子 提交于 2020-01-02 22:18:13
问题 I'm just not getting this: I use Gin in my java GWT app to do DI. The login screen is integrated into the full application window. After the user has logged in I want to inject the user object into other classes like GUI Presenters which I create, so I have some sort of runtime dependency I believe. How do i do that? One solution I can think of is sth like: class Presenter { @Inject Presenter(LoggedInUserFactory userFactory) { User user = userFactory.getLoggedInUser(); } } class