Guice

Guice, Injecting TypeLiteral<T> when using @AssistedInject

杀马特。学长 韩版系。学妹 提交于 2019-12-24 02:17:05
问题 This works: public static class SomeGenericType<T> { private TypeLiteral<T> type; @Inject public SomeGenericType(TypeLiteral<T> type) { this.type = type; } public Class<? super T> getType() { return type.getRawType(); } } Guice automatically injects the TypeLiteral representing String when I do: @Inject SomeGenericType<String> foo; But when trying the same thing with Assisted Inject: public static interface FooFactory<T> { Foo<T> create(String name); } public static class Foo<T> {

Java inject implementation using TypeLiteral

我与影子孤独终老i 提交于 2019-12-24 00:46:02
问题 I have a project that provides an interface, let's call it IImplementMe, which i want to inject into my project. This interface will be implemented by various producers, so I need to inject all implementations. I am trying to use TypeLiteral for this. Here is the code of the producer : @Singleton public class SomeImplementation implements IImplementMe { private final String value; @Inject public SomeImplementation(final SomeOtherConfig configuration) { this.value= configuration.getValue(); }

Mock a superclass constructor

谁说我不能喝 提交于 2019-12-23 17:40:27
问题 I would like to know if I can mock a super class constructors call and its super() calls. For example, I have the following classes class A { A(..) { super(..) } } class B extends A { B(C c) { super(c) } } So, I am planning to unit test some methods in class B, but when creating an instance it does call the super class constructors making it tough to write unit tests. So, how can I mock all the super class constructor calls. Also I would like to mock few methods in class A so that it does

Guice:最好用的依赖注入框架

妖精的绣舞 提交于 2019-12-23 17:32:40
首先在你的maven项目里引入 还可以自动注入Set,Map容器,但是得首先加上扩展库 我没有使用官方的例子,写个最简单的HelloWorld Guice里最常用的两个注解就是@Singleton和@Inject,Singleton表示构建的对象是单例的,Inject表示被标注的字段将使用Guice自动注入。在一般的项目中这两个注解一般可以完成90%以上的装配工作。 Guice需要实例化对象,请确保相应被实例化的对象有默认构造器。 当某个接口有多个实现时,我们使用@ImplementedBy注解在接口定义上,指定接口的具体实现类 如果我们不用Singleton标注,每次获取实例时,Guice会重新构造一个,这个会有反射构造器的性能损耗,在高性能场景下,请谨慎。 我们还可以使用@Named名称指令来指定依赖注入实现 我们不使用字段注入,改用构造器注入,如果我们需要在构造器里做一些特别的初始化工作 日照山太执行首架A320型飞机定检完工交付 来源: CSDN 作者: 大魔王薇薇薇 链接: https://blog.csdn.net/weixin_46023180/article/details/103663535

Is there a nice Play2 way of injecting instances in Play Plugins with Guice

被刻印的时光 ゝ 提交于 2019-12-23 13:07:25
问题 I'm trying to figure out how to inject my classes with Google Guice into a play.api.Plugin. I have implemented Guice to work with my controllers and it works great. I use: "com.google.inject" % "guice" % "4.0-beta", "com.tzavellas" % "sse-guice" % "0.7.1" When a Controller instance is needed the getControllerInstance method in Global will load the appropriate implementation thanks to the injector. Global: object Global extends GlobalSettings { /** * Currently we only want to load a different

Serving static content with Jetty/Jersey/Guice

亡梦爱人 提交于 2019-12-23 12:06:43
问题 Similar to another question (cf. Filtering static content Jersey) I want to serve static content from Jetty. There are several similar questions scattered all around the vast Internet, but most of them do not involve Guice, and those that do are completely out of date. I have an existing service that uses Jersey (1.12) and Guice (3) with the following web.xml : <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema

How does Guice Populate Annotated Fields

久未见 提交于 2019-12-23 11:54:36
问题 For the sake of my own education, I wanted to build a simple Dependency Injection framework that functions similar to the way Google's Guice does. So that when a class is loaded, it pre-populates annotated fields with data from a factory class. I am using Reflections to scan all my factory classes at compile time and save those classes in a static list so that when it comes time to load my classes, I have a reference to my factories that I can then scan methods and return the appropriate data

Guice + classpath scanning

别说谁变了你拦得住时间么 提交于 2019-12-23 09:32:41
问题 I am looking at Guice at the moment and it would appear that its geared towards explicit programmatic building of the context via modules. Now I am fairly used to using annotations to put something into the context and using classpath scanning to build the context. Now I could fairly easily add this "feature" to guice, but I'd rather not reinvent the wheel, so if someone knows if there is an extension that already does this - please say. However, my question is, would I be breaking the

How to configure the Maven dependency for Guice 3.0 for use without AOP?

跟風遠走 提交于 2019-12-23 09:20:05
问题 I have an Android Maven project and want to use Google Guice 3.0 in it. There is a "No-AOP" version of Guice, which is compatible with Android. How can I tell maven to use the "No-AOP" version of guice? Update 1 (03.05.2013 10:46 MSK): When I add the dependency on Google Guice, I get following exception during the build ( mvn clean install ): [INFO] UNEXPECTED TOP-LEVEL EXCEPTION: [INFO] java.lang.IllegalArgumentException: already added: Lcom/google/inject/Abs tractModule; [INFO] at com

What is the best way to use Guice and JMock together?

被刻印的时光 ゝ 提交于 2019-12-23 08:53:27
问题 I have started using Guice to do some dependency injection on a project, primarily because I need to inject mocks (using JMock currently) a layer away from the unit test, which makes manual injection very awkward. My question is what is the best approach for introducing a mock? What I currently have is to make a new module in the unit test that satisfies the dependencies and bind them with a provider that looks like this: public class JMockProvider<T> implements Provider<T> { private T mock;