X-Received: by 2002:a37:a582:: with SMTP id o124mr3548589qke.26.1548785513298; Tue, 29 Jan 2019 10:11:53 -0800 (PST) X-Received: by 2002:a25:2c92:: with SMTP id s140mr57549ybs.4.1548785513027; Tue, 29 Jan 2019 10:11:53 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!v55no1452984qtk.0!news-out.google.com!h3ni10793qtk.1!nntp.google.com!v55no1452974qtk.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Tue, 29 Jan 2019 10:11:52 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=192.176.1.81; posting-account=1c_fOgoAAADuOXlL0A4-T9PUmVHtMSYd NNTP-Posting-Host: 192.176.1.81 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: How to make my library independent using DI From: mike Injection-Date: Tue, 29 Jan 2019 18:11:53 +0000 Content-Type: text/plain; charset="UTF-8" Lines: 103 Xref: csiph.com comp.lang.java.programmer:38719 Hi, I am creating a library that can be used with different java applications. Sometimes I need data from different applications but I want to avoid a direct dependency to these applications. In my library I have the following classes: package scrap; public interface MyDataClass { String productNumber(); String version(); String time(); } and public class MyDataClassImpl implements MyDataClass{ private ExternalDataService externalDataService; public MyDataClassImpl(ExternalDataService externalDataService){ this.externalDataService = externalDataService; } @Override public String productNumber() { //Here we have a dependency to an external library call it x return externalDataService.productNumber(); } @Override public String version() { //Here we have a dependency to an external library call it x return externalDataService.version(); } @Override public String time() { return calcTime(); } private String calcTime () { return ""; } } as well as: package scrap; public interface ExternalDataService { public String productNumber(); public String version(); } Then in the application using this lib I have: package other; import scrap.ExternalDataService; public class AHelper implements ExternalDataService{ @Override public String productNumber() { return methodA(); } @Override public String version() { return methodB(); } public String methodA() { return ""; } String methodB() { return ""; } } I have two questions: 1. How can I inject AHelper into MyDataClassImpl without creating a "hard dependency" without using a DI container? 2. If I used a container like Guice how would that look like? All hints welcome. br, Mike