ServiceLoaderUtil { public R initialise(Class<R> service, Class<? extends R> defaultImplementation, I18nLoader i18n, DocumentAttributes attributes, Class<? extends R>... except) { R renderer = load(service, defaultImplementation, except); renderer.setDocumentAttributes(attributes); renderer.setI18n(i18n); return renderer; } ServiceLoaderUtil();  ServiceLoaderUtil(Iterable<R> services);  ServiceLoaderUtil(Function<Class<R>, Iterator<R>> loader); R load(Class<R> service, Class<? extends R> defaultImplementation, Class<? extends R>... except); R initialise(Class<R> service, Class<? extends R> defaultImplementation, I18nLoader i18n, DocumentAttributes attributes, Class<? extends R>... except);  }
@Test public void shouldInitialise() { I18nLoader i18n = I18nLoader.newInstance(null); DocumentAttributes attributes = new DocumentAttributes(); ServiceLoaderUtil<BaseRenderer> loader = new ServiceLoaderUtil<>(x -> { ArrayList<BaseRenderer> services = new ArrayList<>(1); services.add(new MyService1()); services.add(new MyService2()); services.add(new MyService3(42)); return services.iterator(); }); MyService3 renderer = (MyService3) loader.initialise(BaseRenderer.class, MyService1.class, i18n, attributes, MyService1.class, MyService2.class); assertSame(i18n, renderer.getI18nProvider()); assertSame(attributes, renderer.getDocumentAttributes()); }