有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓服务上的java依赖注入

我正在开发一个Android应用程序,并使用Roboguice 2.0

除了现在,一切都进展得非常顺利。我试图将我的自定义绑定注入到这样的服务中,但它不起作用

    @Inject
private TicketService ticketService;

@Inject
private PerformanceService performanceService;

它们是这样绑定的(首先是自定义应用程序上下文):

 RoboGuice.setBaseApplicationInjector(
            this, 
            RoboGuice.DEFAULT_STAGE,
            Modules.override(RoboGuice.newDefaultRoboModule(this))
                .with(
                    new KoliseoModule(),
                    new DatastoreModule(this),
                    new ServiceModule()
                    ));


 public class ServiceModule extends AbstractModule {

@Override
protected void configure() {
      bind(JsonCheckinTimestampService.class).to(JsonCheckinTimestampServiceImpl.class);
    bind(PerformanceService.class).to(PerformanceServiceImpl.class);
    bind(ShowService.class).to(ShowServiceImpl.class);
    bind(TicketService.class).to(TicketServiceImpl.class);
    bind(ValidationStatisticsService.class).to(ValidationStatisticsServiceImpl.class);
}

}

有什么想法吗

谢谢


共 (1) 个答案

  1. # 1 楼答案

    应用程序的应用程序子类很可能被加载,甚至可能得到保证&;在创建其余实例之前实例化,以便您可以尝试。 创建一个“MyApplication extends Application”类,并在其“onCreate()”中进行设置,不要忘记在清单中声明