标签: guice

DI,Guice和战略模式

假设我有以下基类,Queen和Knight作为它的衍生物.WeaponBehaviour是一个界面.根据具体的GameCharacter类型,我无法弄清楚如何使用Guice注入武器.

public abstract class GameCharacter {
    @Inject
    protected WeaponBehaviour weapon;

    public GameCharacter() {

    }

    public void fight() {
        weapon.useWeapon();
    }

    public void setWeapon(WeaponBehaviour weapon) {
        this.weapon = weapon;
    }
}
Run Code Online (Sandbox Code Playgroud)

java dependency-injection guice strategy-pattern

0
推荐指数
1
解决办法
929
查看次数

Guice - 如何使用绑定注释来构建对象列表

我创建了Guice绑定注释,允许我根据注释绑定一个类的两个不同实例,例如:

bind(Animal.class).withAnnotation(Cat.class).toInstance(new Animal("Meow"));
bind(Animal.class).withAnnotation(Dog.class).toInstance(new Animal("Woof"));
Run Code Online (Sandbox Code Playgroud)

我希望能够创建一个提供List方法的提供程序方法,该方法是我的一个类的依赖项,但是无法弄清楚如何使用这个注释:

@Provider
List<Animal> provideAnimalList() {
    List<Animal> animals = new ArrayList<Animal>();
    animals.add(@Cat Animal.class); // No, but this is what I want
    animals.add(@Dog Animal.class); // No, but this is what I want
    return animals;
}
Run Code Online (Sandbox Code Playgroud)

所以我假设我只能add()在List的方法中使用参数中的注释...但是没有.

我该怎么接近这个?在我看来,简单地对newAnimal类的两个实例更简单,也许这不是如何使用绑定注释.

我很感激在这种情况下最好地使用绑定注释的评论.

谢谢

dependency-injection guice

0
推荐指数
1
解决办法
5493
查看次数

子模块的故障安全注入器。google guice 中的可选绑定

我有一个名为 ChildPlugin 的子模块,我从主模块注入类,如下所示:

public class ChildPlugin {
    private ExampleClass demo;

    @Inject
    public void setDemo(ExampleClass demo) { 
        this.demo = demo;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我不知道主模块是否绑定ExampleClass,如果不是 Guice 在创建注入器时抛出异常。我想要做的是让 Guice 通过null或者Optional.empty如果 ExampleClass 没有绑定。

我没有进入主模块,所以我不能改变粘结剂ExampleClassOptionalBinder,我试图@NullableOptional<ExampleClass>ChildPlugin.setDemo方法,但没有奏效。

java dependency-injection guice optional-binding

0
推荐指数
1
解决办法
2371
查看次数

ScalaTest,Mockito,Guice和PartialMocking

我正在使用Google Guice作为DI框架,我正在为使用Google Guice的课程编写单元测试.我也试图做部分嘲笑.

这是我写的代码

class Test1 {
   def test1() = "I do test1"
}
class Test2 {
   def test2() = "I do test2"
}
class TestPartialMock @Inject()(t1: Test1, t2: Test2) {
   def test3() = "I do test3"
   def createList() : List[String] = List(t1.test1(), t2.test2(), test3())
}
Run Code Online (Sandbox Code Playgroud)

我的目标是为上面的代码编写测试用例,但我只想模拟 test3

我写了这个测试用例

class TestModule extends AbstractModule with ScalaModule with MockitoSugar {
   override def configure() = {
      bind[Test1]
      bind[Test2]
      val x = mock[TestPartialMock]
      when(x.test3()).thenReturn("I am mocked")
      when(x.createList()).thenCallRealMethod()
      bind(classOf[TestPartialMock]).toInstance(x)
   }
}

class PartialMockTest extends FunSpec …
Run Code Online (Sandbox Code Playgroud)

unit-testing scala guice mockito scalatest

0
推荐指数
1
解决办法
861
查看次数

如何将非Guice实例注入Guice模块

我希望能够在外部(例如在Spring中)定义一个简单的Config bean,并将其注入到Guice模块中。

有什么办法可以做到这一点?

public class InjectionTest {

    @Test
    public void test() {
        // In reality this would be externally defined
        Config config = new Config("a", "b");

        AbstractModule module = new AbstractModule() {
            @Override
            protected void configure() {
                bind(Config.class).toInstance(config);
            }
        };

        Injector injector = Guice.createInjector(module);
        Thing instance = injector.getInstance(Thing.class);
    }

    static class Thing {
        final Config config;

        public Thing(Config config) {
            this.config = config;
        }
    }

    static class Config {
        final String a, b;

        public Config(String a, String b) {
            this.a …
Run Code Online (Sandbox Code Playgroud)

guice

0
推荐指数
1
解决办法
40
查看次数

Scala和Annotation

只想在scala中实现Guice

任何示例代码?

scala guice

-2
推荐指数
2
解决办法
2334
查看次数