Friday, May 18, 2012

Wicket & Guice - how to start service on application start

I have Wicket web application and I am using Guice to inject services whatever I want. Some thing like here. Everything works great, but I needed to run some recovery routine on application startup. The only way I found is this:
public class WicketApplication extends WebApplication{
    @Override
 public void init()
 {
  super.init();

        GuiceModule guiceModule = new GuiceModule(usesDeploymentConfig());
        GuiceComponentInjector injector = new GuiceComponentInjector(this, guiceModule);
        getComponentInstantiationListeners().add(injector);
        
        //Here is interesting part
        GuiceInjectorHolder holder = Application.get().getMetaData(GuiceInjectorHolder.INJECTOR_KEY);
        holder.getInjector().getProvider(EmailSenderService.class).get().recover();
    }
}

Enjoy!