Guice

How to avoid dependency injection in Scala?

大憨熊 提交于 2019-12-03 17:03:58
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 B { @Inject public B(D d) { this.d = d; } } public class C { @Inject public C(D d) { this.d = d; } }

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

让人想犯罪 __ 提交于 2019-12-03 16:23:10
问题 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? 回答1: I don't know how I missed this! https://github.com/roboguice/roboguice 来源: https:/

Java error: Found interface … but class was expected

北城以北 提交于 2019-12-03 14:27:55
问题 I am getting a strange runtime error from my code: "Found interface [SomeInterface] but class was expected" How can this happen? How can an interface get instantiated? Update: (In response to some answers) I am compiling and running against the same set of libraries, but I am using Guice to inject a Provider for this particular Interface. The problem went away when I bound an implementation to the interface (seems like the @ImplementedBy annotation was not enough). I was more interested in

Dropwizard and Guice: injecting Environment

大城市里の小女人 提交于 2019-12-03 13:20:59
I am currently building a Dropwizard + Guice + Jersey-based application where the database access is being handled by JDBI for the time being. What I am trying to achieve is to have your typical enterprise architecture, where Resources access Service classes accessing a DAO class that in turn accesses the database. It would be nice to get all this wired up in a proper DI way, although I guess I can build my object graph in the run() method of the application if all else fails. So, I'm running into this problem that has been mentioned here before : Getting a DBIFactory requires both the

Simple Example with Guice Servlets

家住魔仙堡 提交于 2019-12-03 13:01:34
问题 I don't know how to proceed with a simple guice example. After reading the documentation I've done the following: setup the guiceFilter created an injector and instantiated a new ServletModule in a GuiceServletContextListener and added the listener to web.xml bound serve("*.jsp").with(IndexController.class); in configure servlets After I've done that how do I use dependency injection? Let's say I have an index.jsp, IndexController.class (servlet), and two classes called Person and Order with

Guice eager/lazy singleton instantiations

拜拜、爱过 提交于 2019-12-03 12:36:33
I'm having some troubles understanding how Guice's singleton instantiations works. I've read the available documentation (here - http://code.google.com/p/google-guice/wiki/Scopes ), but I still can't figure out some things: 1) I've integrated Guice with Tomcat, and I've set up some bindings in a ServletModule: bind(MyServlet.class).asEagerSingleton(); serve("myUrl").with(MyServlet.class); serve("myOtherUrl").with(MyOtherServlet.class); (where MyOtherServlet class has a @Singleton annotation above it) My intention here was to have two servlets, where one is eagerly instantiated, while the other

Modern Akka DI with Guice

你离开我真会死。 提交于 2019-12-03 12:06:18
Java 8, Guice 4.0 and Akka 2.3.9 here. I am trying to figure out how to annotate my actor classes with JSR330-style @Inject annotations, and then wire them all up via Guice. But literally every single article I have read (some examples below) either uses Scala code examples, a criminally-old version of Guice, or a criminally-old version of Akka: Let It Crash Scala-Guice So, given the following Guice module: public interface MyService { void doSomething(); } public class MyServiceImpl implements MyService { @Override public void doSomething() { System.out.println("Something has been done!"); }

Guice best practices and anti-patterns

六月ゝ 毕业季﹏ 提交于 2019-12-03 10:23:00
I'm not sure if there is merit to this question or not, but are there any best practices and anti-patterns specific to Google Guice ? Please direct any generic DI patterns to this question . I have always felt that constructor injection to final fields is a best practice. It minimizes mutable state and makes the class easier to understand by making the class's formal dependencies explicit. public class MyClass { private final MyDependency dependency; @Inject public MyClass(MyDependency dependency) { this.dependency = dependency; } } Kevin Bourrillion There are some on the Guice project page.

Method interception in Jersey using Guice AOP

℡╲_俬逩灬. 提交于 2019-12-03 10:08:57
问题 Is it possible to use Guice AOP to intercept an annotated method on a Jersey resource? I have a successfully configured Guice integration working with Jersey with respect to Dependency Injection without any problems, however my configured Interceptor is not intercepting my annotated method at all. web.xml <listener> <listener-class>my.package.GuiceConfig</listener-class> </listener> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter

How to define order of method interceptors in Guice?

穿精又带淫゛_ 提交于 2019-12-03 09:24:52
问题 Sometimes there's a need to know the order of method interceptors that intercept a method call in Guice. A simple example scenario would be to use guice-persist provided @Transactional method interceptor with a custom @Retry method interceptor. The retry interceptor must be run outside of the transactional interceptor to make sure the retries are not executed within the same transaction. In Spring you could use the Ordered interface for the interceptor to make sure the transaction interceptor