【问题标题】:How to implement and test mapbinding correctly with Guice and Play framework如何使用 Guice 和 Play 框架正确实现和测试地图绑定
【发布时间】:2013-07-28 00:36:03
【问题描述】:

我刚开始使用 Guice 和 Play,所以我想这是一个很长但基本的问题。我在这里查看了指南:http://eng.42go.com/play-framework-dependency-injection-guice/,但我不知道为什么我的代码会失败。

首先我有一个全局注入器:

public class GlobalInjector {
    private static Injector guiceInjector;
    private static List<AbstractModule> modules = new ArrayList<AbstractModule>();

    public static Injector getInjector() {
        return guiceInjector;
    }

    public static loadModules() {
        guiceInjector = Guice.createInjector(modules);
    }

    public static addModule(AbstractModule module) {
        modules.add(module);
    }
}

我还通过扩展 GlobalSettings 类(也修改了 application.global)将 Guice 添加到 Play 中

public class GuiceExtendedSettings extends GlobalSettings {
    @Override
    public void onStart(Application app) {
        GlobalInjector.loadModules();
    }

    @Override
    public <A> A getControllerInstance(Class<A> controllerClass) {
         return GlobalInjector.getInjector().getInstance(controllerClass);
    }
}

然后我将我的测试模块作为 Play 中的插件(省略了一些必需的方法,因为它们在本文中什么都不做):

public class TestModule extends AbstractModule implements Plugin {

    @Override
    public void configure() {
        // Worker is a simple class
        Worker worker = new SimpleWorker();
        MapBinder<String, Worker> mapBinder = MapBinder.newMapBinder(binder(), String.class, Worker.class);
        mapBinder.addBinding(worker.getName()).toInstance(worker);
    }

    @Override
    public void onStart() {
        GlobalInjector.addModule(this);
    }
}

Worker 是一个简单的接口:

public interface Worker {
    public String getName();
    public String getResult();
}

SimpleWorker:

public class SimpleWorker implements Worker {
    public String getName() {
        return "SimpleWorker";
    }

    public String getResult() {
        return "works";
    }
}

这里是显示控制器逻辑的代码片段:什么都没有,只是在注入的地图中打印所有工作人员结果

public class TestController extends Controller {
     @Inject
     Map<String, Worker> workers;

     public Result showWorkers() {
         StringBuilder sb = new StringBuilder();
         for (Worker worker : workers) {
             sb.append(worker.getName() + ": " + worker.getResult() + "</br>");
         }
         return ok(sb.toString()).as("text/html");
     } 
}

好的。为了完成这项工作,我在 play.plugins 中加入了以下行:

100:test.TestModule

我的想法是: Play 加载插件 (TestModule) -> TestModule 将自身添加到 GlobalInjector -> GlobalInjector 创建 Guice 注入器 -> Guice 将地图注入控制器

但是结果是 Guice 没有注入地图。地图还是null

另外我应该如何测试它?(即如何向该地图注入不同的工作人员?我在上面的代码中硬编码了该部分。但我正在寻找一种动态方式使用不同的模块。)

public class Test {
    @Test
    public void testInjector() {
        running(fakeApplication(), new Runnable() {
           public void run() { 
               // how can I inject using different modules here?
           }
        });

    }
} 

【问题讨论】:

    标签: java dependency-injection playframework guice


    【解决方案1】:

    您需要使用 fakeApplication 辅助方法,该方法允许您指定全局设置对象和其他插件。请参阅http://www.playframework.com/documentation/api/2.1.x/java/play/test/Helpers.html#fakeApplication(java.util.Map,%20java.util.List,%20play.GlobalSettings) 了解更多信息。

    但基本上,您的测试应该如下所示:

    public class Test {
        @Test
        public void testInjector() {
           Map<String, Object> config = new HashMap<String, Object>();
           // add any additional config options, e.g. in-memory db
           List<String> plugins = new ArrayList<String>();
           plugins.add("full.package.name.TestModule");
           GlobalSettings global = null;
           try {
               global = (GlobalSettings) Class.forName("full.package.name.GuiceExtendedSettings").newInstance();
           } catch(Exception e) {}
    
           running(fakeApplication(config, plugins, global), new Runnable() {
               public void run() { 
                   // do some assertions
               }
            });
        }
    }
    

    您还需要确保 guice 实例化测试控制器,否则工作人员映射不会被注入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多