我一直在努力理解并设置Dagger来处理Android项目的依赖注入.我的单一(没有双关语)目标是创建我可以在我的应用程序中访问的单例对象.我已经在初始活动中成功设置了对象.我被困在哪里是从其他类访问这些对象.这是我到目前为止的设置:
初始App活动
public class SplashScreenActivity extends AppCompatActivity { @Inject SessionKeyExchangerService exchangerService; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_splash_screen); AppComponent component = DaggerAppComponent.builder().appModule(new AppModule()).build(); // establish the session ID as a singleton object exchangerService = component.provIDeSessionKeyExchangerService(); // test whether I can access the singleton from another class exchangerService.sendEncryptedKeyToServer(); }
组件类
@Singleton@Component(modules = {AppModule.class})public interface AppComponent { SessionKeyExchangerService provIDeSessionKeyExchangerService(); AESCipherService provIDeCipherService();}
模块类
@Modulepublic class AppModule { @ProvIDes @Singleton AESCipherService provIDeCipherService() { return new AESCipherService(); } @ProvIDes @Singleton SessionKeyExchangerService provIDeSessionKeyExchangerService(AESCipherService service) { return new SessionKeyExchangerService(service); }}
AESCipherService
public class AESCipherService { private Long sessionID; public AESCipherService() { sessionID = makeSessionID(); Log.d(Constants.TAG, "Session ID: " + Long.toString(sessionID)); } private Long makeSessionID() { // this generates a random unsigned integer in the space {0, 2^32-1) Random random = new Random(); return random.nextLong() & 0xffffffffL; } public Long getSessionID() { return sessionID; }}
SessionKeyExchanger类
public class SessionKeyExchangerService { private static SessionKeyExchangerService exchanger; private AESCipherService cipherService; @Inject public SessionKeyExchangerService(AESCipherService cipherService) { this.cipherService = cipherService; } public voID sendEncryptedKeyToServer () { // the next line is almost certainly part of the problem // but I don't kNow how to fix!!! AppComponent component = DaggerAppComponent.builder().appModule(new AppModule()).build(); AESCipherService cipherService = component.provIDeCipherService(); Long sessionID = cipherService.getSessionID(); Log.d(Constants.TAG, "singleton verification: " + (Long.toString(sessionID))); }
这是一些示例输出:
Session ID: 217186720
singleton verification: 790090968
显然,我没有访问同一个对象.我意识到至少部分问题源于我在尝试获取对AppComponent类的引用时在AESCipherService中调用new运算符的方式,但我不知道如何获取此引用另一种方式.
我该如何解决?谢谢!
解决方法:
AppComponent component = DaggerAppComponent.builder().appModule(new AppModule()).build();
Nononononono.这不会是一个单身人士.范围提供程序仅适用于每个组件,这意味着您必须在整个应用程序中使用单个组件,以使@Singleton范围内的模块实际共享同一范围提供程序.在这种情况下,您每次创建活动时都会创建一个新组件.
你需要像这样创建它们:
public enum Injector { INSTANCE; private AppComponent appComponent; static { INSTANCE.appComponent = DaggerAppComponent.create(); } public getAppComponent() { return appComponent; }}
你也可以子类化Application并在onCreate()中创建一个.
也
@Singleton@Component(modules = {AppModule.class})public interface AppComponent { SessionKeyExchangerService provIDeSessionKeyExchangerService(); AESCipherService provIDeCipherService(); voID inject(SplashScreenActivity splashScreenActivity); //does NOT support base class injection! Concrete classes only!}
然后
public class SplashScreenActivity extends AppCompatActivity { @Inject SessionKeyExchangerService exchangerService; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_splash_screen); Injector.INSTANCE.getAppComponent().inject(this); // establish the session ID as a singleton object // exchangerService = component.provIDeSessionKeyExchangerService(); //totally not needed // test whether I can access the singleton from another class exchangerService.sendEncryptedKeyToServer();
此外,您正在使用基于@ Module的实例创建,因此在构造函数中丢失@Inject
@Injectpublic SessionKeyExchangerService(AESCipherService cipherService) { this.cipherService = cipherService;}
并且
public voID sendEncryptedKeyToServer () { // the next line is almost certainly part of the problem // but I don't kNow how to fix!!! //AppComponent component = DaggerAppComponent.builder().appModule(new AppModule()).build(); //you don't need this here at all //AESCipherService cipherService = component.provIDeCipherService(); //already provIDed in constructor
总结 以上是内存溢出为你收集整理的java – Dagger – 从其他类访问Singleton对象全部内容,希望文章能够帮你解决java – Dagger – 从其他类访问Singleton对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)