{"id":5196,"date":"2014-03-30T19:33:33","date_gmt":"2014-03-30T19:33:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-dependency-injection-with-multiple-implementations-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:33:33","modified_gmt":"2014-03-30T19:33:33","slug":"java-dependency-injection-with-multiple-implementations-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-dependency-injection-with-multiple-implementations-collection-of-common-programming-errors\/","title":{"rendered":"Java Dependency Injection with Multiple Implementations-Collection of common programming errors"},"content":{"rendered":"<p>I assume you are running on the Java SE platform (as opposed to a Java EE platform) in which case I would highly recommend HK2 (see https:\/\/hk2.java.net\/2.2.0\/). It has a lot of support for efficiently instantiating services and it is certainly the case that multiple implementations of the same contract can be available at runtime. Then at runtime there are a whole manner of mechanisms that you can use to choose which particular implementation will satisfy the dependency (i.e., service ranking or assisted injection etc)<\/p>\n<p>For build time with hk2 you can create &#8220;inhabitant&#8221; files that describe services to the point where they can be satisfied at runtime <em>without<\/em> classloading all of them (only the one that is picked will be classloaded if you do it properly). This can be a <em>huge<\/em> performance boost at boot time of your application (if that sort of thing matters to you).<\/p>\n<p>If you are running on a Java EE platform you can also use HK2, but you should then also give a long look at CDI. Both CDI and HK2 are implementations of JSR-330, and so both work with javax.inject API<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I assume you are running on the Java SE platform (as opposed to a Java EE platform) in which case I would highly recommend HK2 (see https:\/\/hk2.java.net\/2.2.0\/). It has a lot of support for efficiently instantiating services and it is certainly the case that multiple implementations of the same contract can be available at runtime. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5196","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5196"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5196\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}