用GAE基本缩放实例使用Guice进行依赖注入

时间:2016-09-23 18:30:40

标签: google-app-engine guice

我有一个在GAE(Basic Sc​​aling)上运行的应用程序,它使用Guice进行依赖注入。

Guice Listener

public class GuiceListener extends GuiceServletContextListener {
private ServletContext servletContext = null;

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    servletContext = servletContextEvent.getServletContext();
    super.contextInitialized(servletContextEvent);
}   

/**
 * Function to create Guice Injector 
 */
@Override
protected Injector getInjector() {    
    return Guice.createInjector(
            new GuiceServletModule(),        
            new ServiceInjectionModule()
            );
}
}

Guice Servlet模块

public class GuiceServletModule extends GuiceSystemServiceServletModule {
@Override
protected void configureServlets() {        
    super.configureServlets();
    Logger.info("Guice configure servelet");
    filter("/*").through(NamespaceFilterCustom.class);

    Map<String, String> params = new HashMap<>();
    params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.my.service.endpoint");
    params.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
    //you can create your own filters to handle request and response differently
    params.put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, GZIPContentEncodingFilter.class.getName());
    params.put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, GZIPContentEncodingFilter.class.getName());
    serve("/*").with(GuiceContainer.class, params);             
}
}

服务注入模块

public class ServiceInjectionModule extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(ServiceImpl.class);
}
}

我的端点类需要注入“Service”对象。与GAE Auto Scaling一起使用时,该应用程序运行正常。但是,当应用程序在GAE Basic Sc​​aling上运行时,不会注入“Service”对象。访问“服务”时,抛出空指针异常

0 个答案:

没有答案