【问题标题】:What is the Google App Engine equivalent of Android's 'Application' class? for dependency injection与 Android 的“应用程序”类等效的 Google App Engine 是什么?用于依赖注入
【发布时间】:2015-07-01 19:58:34
【问题描述】:

Android 允许我们扩展 Application,这是存储我们的单例的好地方,因此它们可以轻松地在应用程序的不同页面(活动)中重复使用。

Google App-Engine 相当于什么?

我正在使用 dagger,我不想为每个端点创建一个新的对象图。

【问题讨论】:

    标签: android google-app-engine google-cloud-platform dagger-2


    【解决方案1】:

    一种选择(尽管我不确定它是最好的)就是自行注入您的 Cloud Endpoints 类(通过静态字段注入)。

    假设您正在 ServletContextListener 中初始化对象图,因此您可以在调用端点方法之前在那里这样做。

    Dagger 1.x 支持模块注解上的 staticInjections(为您执行此操作),但这显然在 2.x 中不再支持。

    【讨论】:

    • 不幸使用匕首2
    • 然后你就可以在你初始化你的对象图之后手动自注入了。
    • 这是我的问题。假设我在 ServletContextListener 中创建了我的 Singleton component。现在我在我的端点类中。如何获得对 component 对象的引用?在 Android 中,我会从我的 activity 中调用 getApplication()。端点不扩展类,所以我看不到任何可以使用的 getServletContext() 方法。
    • 哦,等一下,我可以在我的ServletContextListener 中将component 设为静态单例
    • ...或者在您的端点类中放置一个静态字段并从您拥有对象图的 ServletContextListener 设置它。那么你的端点类就不需要知道 ServletContextListener。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2012-05-27
    • 2021-02-09
    • 2011-08-21
    • 2011-09-07
    相关资源
    最近更新 更多