Guice

Guice: inject different implementation depending on who is getting it?

痴心易碎 提交于 2019-12-20 02:43:11
问题 I have two third-party classes, both of which take an implementation of an Authorizer interface. I need to inject each with a different implementation. If I do an @Provides , how can I implement it so that it provides the implementation required at run time? The provider has no idea who is asking for the injection. In theory I could use @Named , but I can't modify the code being injected. I want to do something like: bind(Authorizer.class).to(ImplA.class).for(SomeClass.class) bind(Authorizer

Jersey, Guice and Hibernate - EntityManager thread safety

烂漫一生 提交于 2019-12-19 12:20:06
问题 I have used this tutorial them same way in my application: http://www.benmccann.com/hibernate-with-jpa-annotations-and-guice/ My app is JAX-RS web service which will receive many concurrent requests and make updates to database. GenericDAOImpl.java implementation: public class GenericDAOImpl<T> implements GenericDAO<T> { @Inject protected EntityManager entityManager; private Class<T> type; public GenericDAOImpl(){} public GenericDAOImpl(Class<T> type) { this.type = type; } @Override public

Multi tenancy with Guice Custom Scopes and Jersey

主宰稳场 提交于 2019-12-19 08:56:57
问题 I am in the process of developing a multi tenancy application with Jersey using Guice for DI (I also use Dropwizard but I don't think it matters here). One thing that bothers me is the fact that some kind of tenancy_id is all over the place in my application. Most of my URLs look like this: /:tenancy_id/some_resource/do_stuff . So the method in my Jersey resource is called with the tenancy_id and hands it over to a service which calls other services and so on. These services are configured

Changing Guice bindings at runtime

我只是一个虾纸丫 提交于 2019-12-19 05:24:14
问题 I would like to be able to change the Guice injections at runtime to support multiple injections based on user input. This is what I would like to achieve: public interface IDao { public int someMethod(); } public class DaoEarth implements IDao { @Override public int someMethod(){ ... } } public class DaoMars implements IDao { @Override public int someMethod(){ ... } } public class MyClass { @Inject private IDao myDao; public int myMethod(String domain) { //If Domain == Earth, myDao should be

TypeLiteral injection with reflection

纵饮孤独 提交于 2019-12-18 16:52:54
问题 Context : java using guice (last version) Hi everybody, is it possible to inject some TypeLiteral with Guice by this way : public MyClass<?,?> getMyClass(Injector injector, Class<?> a, Class<?> b) { //how to Inject MyClass with type a & b ? //e.g : injector.getInstance(MyClass<"a class","b class">) } public interface MyClass<S,T> { public T do(S s); } public class ClassOne implements MyClass<String,Integer> { public Integer do(String s) { //do something } } Module : bind.(new TypeLiteral

How to avoid having injector.createInstance() all over the place when using guice?

此生再无相见时 提交于 2019-12-18 13:01:24
问题 There's something I just don't get about guice: According to what I've read so far, I'm supposed to use the Injector only in my bootstrapping class (in a standalone application this would typically be in the main() method), like in the example below (taken from the guice documentation): public static void main(String[] args) { /* * Guice.createInjector() takes your Modules, and returns a new Injector * instance. Most applications will call this method exactly once, in their * main() method. *

The guice AbstractModule install method

喜你入骨 提交于 2019-12-18 12:47:34
问题 What does the method install() from the AbstractModule class do? Can someone explain it to me? From the docs I read from the guice site all I could get was: Uses the given module to configure more bindings. Configure what bindings exactly? The bindings from the installed module or the bindings of the class that invoked the install method? 回答1: install allows for composition: Within its configure method, FooModule may install FooServiceModule (for instance). This would mean that an Injector

Guice and interface that has multiple implementations

谁说胖子不能爱 提交于 2019-12-18 11:39:07
问题 If I have interface Validator and multiple implementations for this interface. How can I inject any of the multiple implementations with Guice? Now I know that I can use following code to inject one, but it allows only one implementation: public class MyModule extends AbstractModule { @Override protected void configure() { bind(Validator.class).to(OneOfMyValidators.class); } } What I would like to do is: Validator v1 = injector.getInstance(Validator1.class); Validator v2 = injector

How to specify a classifier in a gradle dependency's dependency?

北慕城南 提交于 2019-12-18 11:00:09
问题 Say I want to add guice-assistedinject as a dependency in my project. It specifies the guice artifact as a dependency itself. How do I tell it to use the no_aop version of guice? I know I can do the following, but can I do it in one step without excluding the guice module? dependencies { compile (group: 'com.google.inject.extensions', name: 'guice-assistedinject', version: '3.0') { exclude module: 'guice' } compile group: 'com.google.inject', name: 'guice', version: '3.0', classifier: 'no_aop

Guice with multiple concretes…picking one of them

孤街醉人 提交于 2019-12-18 09:40:04
问题 I am injection multiple concretes of the same interface. I figured out the Guide "code it up" convention. My code currently spits out [INFO] App - About to ship. (abc) [INFO] App - ShipperInterface . (FedExShipper) [INFO] App - ShipperInterface . (UpsShipper) [INFO] App - ShipperInterface . (UspsShipper) So I have the multiple "shippers" at my fingertips. Note the method: public void ProcessOrder(String preferredShipperAbbreviation, Order ord) { I'm trying to figure out the best way to use