I'm writing a web application with a framework that calls the run()
method of my Application
class and passes in an object of type Environment
.
Other objects I'm writing depend on this Environment
class, as they need to call its register()
and metrics()
methods.
The problem is that I create the object graph in the main()
function of my application, like this:
public class MainApplication extends Application<MainConfiguration> {
private final ConnectionPool sharedPool;
public static void main(String[] args) throws Exception {
MainApplication mainApplication = DaggerMainApplicationComponent.create()
.createApplication();
mainApplication.run(args);
}
@Inject public MainApplication(ConnectionPool connectionPool) {
super();
sharedPool = connectionPool;
}
@Override
public void run(MainConfiguration configuration, Environment environment) throws Exception {
// Here is where I have access to the environment variable
}
So, by the time MainApplication
is constructed by Dagger, the environment
variable is not ready. It is only when run()
is called that it is available.
Is there a way to inject this variable into the object graph at that point?