Dependency Injection
Dependency Injection or Inversion of Control (IoC) is done with google guice (https://github.com/google/guice)
MySvc.java:
public interace MySvc { public void myServiceMethod() }
MySvcImpl.java
public class MySvcImpl implements MySvc { public void myServiceMethod() { System.out.println("It works!"); } }
MyGuiceModule.java:
public class MyGuiceModule extends com.google.inject.AbstractModule { @Override public void configure() { bind(MySvc.class).to(MySvcImpl.class).in(com.google.inject.Singleton.class); } }
MyPublisher.java:
import com.google.inject.Guice; import com.google.inject.Inject; public class MyPublisher extends Publisher { private transient MySvc mySvc; private String someJobConfig @DataBoundConstructor public MyPublisher(String someJobConfig) { // no constructor injection this.someJobConfig = someJobConfig; } @Inject public void setMySvc(MySvc mySvc) { this.mySvc = mySvc; } @Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) { if (mySvc == null) { Guice.createInjector(new MyModule()).injectMembers(this); } // ... mySvc.myServiceMethod(); // "it works!" }