Guice

Guice Beginner - How to bind concrete classes?

倖福魔咒の 提交于 2019-12-03 06:28:59
问题 I have this class: public class House { private final Door door; private final Window window; private final Roof roof; @Inject public House(Door door, Window window, Roof roof) { this.door = door; this.window = window; this.roof = roof; } } Where Door , Window and Roof are concrete classes. Now if I want to implement a Module for this scenario, I would do it like this: public class HouseModule extends AbstractModule { @Override protected void configure() { bind(Door.class).to(Door.class);

Using Guice to inject dependencies into an Android activity's constructor

两盒软妹~` 提交于 2019-12-03 06:22:38
Does anybody know of a way to use Guice to inject dependencies into the constructor of an Activity in Android? It looks like activities normally have only the default constructor so that the platform can easily create a new instance. While it is easy enough to have a singleton to reference the injector and get dependencies that way it is less clean and introduces a bit of static state. Any suggestions? benstpierre I don't know how I missed this! https://github.com/roboguice/roboguice 来源: https://stackoverflow.com/questions/2525969/using-guice-to-inject-dependencies-into-an-android-activitys

Builder pattern vs. Dependency Injection (for instance via Guice)

不羁的心 提交于 2019-12-03 06:15:24
I'm developing a simple tree-structured database and I'm usually setting dependencies or optional settings via a Builder (Builder pattern). Now I'm not sure when to use for instance Guice, when to use the Builder pattern and when to use a static factory method instead of the constructor itself. I've read Effective Java several times and I think it mentions at least a lot of advantages for not exposing the constructor. It's time to reread ;-) So, do you know of cases which are clearly distinguishable? And shouldn't I expose the constructor? Thus for instance in every case write public static

How to scan classes for annotations?

风流意气都作罢 提交于 2019-12-03 05:19:11
I have a plain jane servlets web application, and some of my classes have the following annotations: @Controller @RequestMapping(name = "/blog/") public class TestController { .. } Now when my servlet applications starts up, I would like to get a list of all classes that have the @Controller annotation, and then get the value of the @RequestMapping annotation and insert it in a dictionary. How can I do this? I'm using Guice and Guava also, but not sure if that has any annotation related helpers. Jacob Schoen You can use the Reflections library by giving it the package and Annotation you are

How to hook Jackson ObjectMapper with Guice / Jersey

落花浮王杯 提交于 2019-12-03 05:15:44
问题 I can't seem to get my Jackson ObjectMapper Module registered correctly. I'm using a Guice + Jersey + Jackson (FasterXML) stack. I've followed how to customise the ObjectMapper based on various question here. In particular, I have a ContextResolver declared, marked as an @javax.ws.rs.ext.Provider and a @javax.inject.Singleton. I have a GuiceServletContextListener along the lines of: @Override protected Injector getInjector() { Injector injector = Guice.createInjector(new DBModule(dataSource),

Implementing dynamic plugins in Java

北战南征 提交于 2019-12-03 03:49:22
问题 I'd like to implement a dynamic plugin feature in a Java application. Ideally: The application would define an interface Plugin with a method like getCapabilities() . A plugin would be a JAR pluginX.jar containing a class PluginXImpl implementing Plugin (and maybe some others). The user would put pluginX.jar in a special directory or set a configuration parameter pointing to it. The user should not necessarily have to include pluginX.jar in their classpath. The application would find

How does guice's TypeLiteral work?

◇◆丶佛笑我妖孽 提交于 2019-12-03 02:42:31
问题 How does Guice's TypeLiteral overcome the Java generic types erasure procedure? It works wonders but how is this accomplished? 回答1: The trick that is used here is that the signatures of generic super types are stored in subclasses and thus survive erasure. If you create an anonymous subclass new TypeLiteral<List<String>>() {} Guice can call getClass().getGenericSuperclass() on it and get a java.lang.reflect.ParameterizedType on which exists a method getActualTypeArguments() to get List<String

Dependency Injection in OSGI environments

别来无恙 提交于 2019-12-03 02:37:20
问题 First some background: I'm working on some webapp prototype code based on Apache Sling which is OSGI based and runs on Apache Felix. I'm still relatively new to OSGI even though I think I've grasped most concepts by now. However, what puzzles me is that I haven't been able to find a "full" dependency injection (DI) framework. I've successfully employed rudimentary DI using Declarative Services (DS). But my understanding is that DS are used to reference -- how do I put this? -- OSGI registered

Dependency injection using Guice with the DAO pattern

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: For a small side project I'm working on I've been trying to implement something of a DAO pattern for my interactions with the DB, and have started using Guice (for my first time) to handle the DI for me. Right now I have this class hierarchy: DAOImpl takes a reference to a class type so my database client (mongo/morphia) can do some initialization work and instantiate a BasicDAO provided by morphia. Here's snippets of the relevant classes: public class DAOImpl<T> implements DAO<T> { private static final Logger LOG = LoggerFactory.getLogger

Accessing Guice injector in its Module?

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am extending Guice's AbstractModule and inside of the extending class I need access to Guice's injector. It that possible, if yes, how? 回答1: This is an unusual request. Modules are more like config files than logic files: The Module is read to create the Injector, and then as soon as the Injector is created the module has done its job. For a simple Module, the Injector literally doesn't exist until the Module is ready to be discarded. In any case, rather than requesting an Injector to get class X, you should typically request a Provider .