I'm trying to refactor some code that relied upon singleton objects, which I am realizing are problematic in Java. My goal is to create a thread-safe instantiation of my AES cipher class that I can access throughout my app.
Following a couple of tutorials about Guice, I have this very basic scenario:
Service Class
@Singleton
public class AESCipherService { // service class
@Inject
AESCipherService() {}
public void makeKeys() {
Log.d(Constants.TAG, "aes cipher generating keys");
}
}
Module Class
This is confusing to me since my service class has no subclasses, but all of the examples that I see are about binding a service super class to one of its subclasses.
public class AppInjectorModule extends AbstractModule {
@Override
protected void configure() {
bind(AESCipherService.class);
}
}
Application Class
public class MyApplication {
private AESCipherService cipher;
@Inject
public void setCipher(AESCipherService cipher) {
this.cipher = cipher;
}
public void makeKeys() {
cipher.makeKeys();
}
}
And finally, the activity onCreate
where this is all supposed to launch from:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Log.d(Constants.TAG, "dependency injection test starting");
Injector injector = Guice.createInjector(new AppInjectorModule());
// EDIT: added this line, but it never prints!
Log.d(Constants.TAG, "injector instantiated");
// instantiates the singleton object ???
MyApplication app = injector.getInstance(MyApplication.class);
app.makeKeys();
}
Unfortunately, the only output that I get is dependency injection test starting
. I am not seeing aes cipher generating keys
, as I am hoping.
Where am I going wrong with dependency injection?
EDIT: I added a few print statements to see where this is failing. It seems that it never returns from instantiating the injector object. Any idea why this is failing? It clearly explains why my service class isn't running.