Conditionally inject modules

27 views
Skip to first unread message

Martin O'Donnell

unread,
May 20, 2021, 11:14:07 AM5/20/21
to dropwizard-user

My Component and and application file are getting very large and the same code base serves multiple brands. We want to conditionally inject modules depending on certain config. Is there any way to do this.

Our current code base looks something like this.

public class SupportPortalApplication extends Application<SupportPortalConfiguration> { ... private void initializeComponent() { if (component == null) { component = DaggerSupportPortalComponent.builder() .moduleOne(new ModuleOne()) .moduleTwo(new ModuleTwo()) .moduleThree(new ModuleThree()) .build(); } } private void registerEndpoints(Environment environment) { environment.jersey().register(component.getResourceModule1()); environment.jersey().register(component.getResourceModule2()); environment.jersey().register(component.getResourceModule3()); } ... } @Singleton @Component(modules = {ModuleOne.class, ModuleTwo.class, ModuleThree.class}) public interface SupportPortalComponent { ModuleOne getModuleOne(); ModuleTwo getModuleTwo(); ModuleThree getModuleThree(); }

I want to be able to conditionally load module three, along with its routes. It is easy to just remove the .moduleThree(new ModuleThree()) from the initializeComponent function conditionally but I would need a way to remove it as a module in the SupportPortalComponent. Is there any way to do this?

DropwizardVersion: "1.3.5"
DaggerVersion:"2.29.1"

Github Question:

https://github.com/dropwizard/dropwizard/issues/3982

Reply all
Reply to author
Forward
0 new messages