Consider this example:
public class GuiceExample {
@Inject
private IDataManager dataManager;
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(IDataManager.class).to(DataManager.class);
}
});
GuiceExample example = injector.getInstance(GuiceExample.class);
example.dataManager.printData();
}
public static interface IDataManager {
void printData();
}
public static class DataManager implements IDataManager {
@Override
public void printData() {
System.out.println("I print data.");
}
}
}
Is it possible to do something right after GuiceExample
class has been initialized (the fields are injected)?
I have tried adding a @PostConstruct
method just in case...but it is not being called.
@PostConstruct
private void fieldsAreInjected() {
System.out.println("Checking if datamanager is null:");
System.out.println(dataManager == null);
}
I tried to find anything on the web, but got nothing. Is it possible or the @Inject
on the constructor is the only way?