Guice

How to avoid dependency injection in Scala?

孤人 提交于 2019-12-21 05:18:13
问题 I read Dependency Injection Without the Gymnastics PDF which indicates there's no need for any fancy DI framework, but it's beyond my grasp (at least without concrete examples). I'll try watching Dependency Injection Without the Gymnastics and Dead Simple Dependency Injection when I have a chance. Using Guice in Java, if A depends on both B and C and both B and C depend on D, one would have something like: public class A { @Inject public A(B b, C c) { this.b = b; this.c = c; } } public class

Mimicking Spring profiles in Guice

三世轮回 提交于 2019-12-21 03:52:52
问题 In Spring, if I want to have one set of objects for production, and another for local development/testing. I could use the @Profile annotation to designate the different classes, and switch between them by providing a system property when starting the app. Is there anything like this in Guice, or do I need to manually check some property myself and load a different set of modules when bootstrapping my Injector ? 回答1: You'll need to identify the environment yourself, and choose which modules

Generic @Inject'd fields in an abstract superclass

别来无恙 提交于 2019-12-20 11:17:12
问题 Consider a MVP-ish set of types. An abstract Presenter exists, with a View interface: public interface View { //... } public abstract class AbstractPresenter<V extends View> { @Inject V view; //... } Then, lets have a specific concrete presenter subclass, with its view interface and implementation: public interface LoginView extends View { //... } public LoginPresenter extends AbstractPresenter<LoginView> { //... } public class LoginViewImpl implements LoginView { //... } In a Dagger module,

How to @Inject into existing object hierarchy using Guice?

天大地大妈咪最大 提交于 2019-12-20 09:59:25
问题 I have an existing object hierarchy where some objects have fields that need to be injected. Also there are some other objects that are constructed using Google Guice and need to be injected with references to some objects from previously described object hierarchy. How do I do such kind of injection with Guice? The problem is that objects from existing hierarchy were not constructed using Guice, and therefore are not subject to inject process by default. There is, of course injector

Has anyone used ServiceLoader together with Guice?

放肆的年华 提交于 2019-12-20 08:51:19
问题 I keep wanting to try this on a larger scale with our app + build system, but higher priorities keep pushing it to the back burner. It seems like a nice way to load Guice modules and avoids the common complaint about "hard coded configuration". Individual configuration properties rarely change on their own, but you will almost always have a set of profiles, usually for different environments (Debug, Production, etc). ServiceLoader lets you pull a list of all implementations defined as a

Injecting Collection of Classes with Guice

我的未来我决定 提交于 2019-12-20 08:48:53
问题 I'm trying to inject things with Google Guice 2.0 and I have the following structure: FooAction implements Action BarAction implements Action I then have an ActionLibrary with the following constructor: ActionLibrary (List<Action> theActions) When I request an instance of ActionLibrary from Guice, I would like Guice to identify both of the registered Action classes (FooAction, BarAction) and pass them into the constructor. The motivation here being that when I add a third action BazAction, it

Embedded Jetty handling urls to serve content

老子叫甜甜 提交于 2019-12-20 06:04:26
问题 I am using embedded Jetty with Guice and am wondering the best way to handle serving my single page application. I wish for Jetty to handle requests like so (in priority order): /socket must be handled by the websocket servlet /fs/read/* , anything that matches this url I need to be handled by a custom servlet /* , anything that matches this url should be served from the /web on the classpath of my Java application assuming it isn't handled by the above. If the resource does not exist, then

Configure an object provided by a Guice Module

醉酒当歌 提交于 2019-12-20 04:24:40
问题 I have a Module that provides a JDBI DBI instance like this: @Provides @Singleton DBI dbi(DataSource dataSource) { return new DBI(dataSource); } In another module I want to call some initialization methods on that DBI instance (configuring support for particular data types). It's not appropriate logic to put in the JDBI module itself as it's application specific not general to any application using JDBI. Is there a hook for me to do that kind of "extra" configuration? I tried using the

Dynamically generating injections by annotation

守給你的承諾、 提交于 2019-12-20 04:16:22
问题 Suppose I have a class that looks like this: public class MyClass { @Inject public MyClass(@Foo("whatever") Bar dependency) { // ... } } And I wanted to have some custom logic that can see we're injecting an object of type Bar with an annotation of type @Foo("whatever") and construct a corresponding Bar object...something like a Guice Provider , but that gets more context information about the injection site. Does Guice let me do something like that? 回答1: What you're describing isn't possible

Dependency injection using Guice with the DAO pattern

别来无恙 提交于 2019-12-20 03:21:37
问题 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