{"id":1364,"date":"2011-05-31T18:42:59","date_gmt":"2011-05-31T18:42:59","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1364"},"modified":"2011-05-31T18:48:10","modified_gmt":"2011-05-31T18:48:10","slug":"iterating-cdi-bean-types","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1364","title":{"rendered":"Iterating CDI Bean Types"},"content":{"rendered":"<p>When developing plugin architectures with IOC, it is useful to be able to iterate over the available beans of a given type which are known to the IO container.<\/p>\n<p>In CDI, this is easy to do, but it is not obvious how from the Weld documentation unless you do a bit of digging and searching.<\/p>\n<p><a href=\"http:\/\/javaboutique.internet.com\/tutorials\/cdi-qualifiers\/index-2.html\"><strong>This post here<\/strong><\/a> gives a sample application which does it. The crucial code snippet which does the business is here:-<\/p>\n<p style=\"padding-left: 30px;\">private @Named @Produces ArrayList&lt;String&gt; wilsons = new ArrayList&lt;String&gt;();<br \/>\n@Inject<br \/>\nvoid initWilsonRacquets(@Any Instance&lt;WilsonType&gt; racquets) {<br \/>\n\u00a0\u00a0\u00a0 for (WilsonType racquet : racquets) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 wilsons.add(racquet.getWilsonRacquetType());<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n}<\/p>\n<p>The intance keyword (used with @Any) exposes an iterator which lets you iterate the available beans. In the above sample they are added to a collection, but you could equally call methods on them as you iterate if you don\u2019t need to keep them.<\/p>\n<p>The Weld documentation describes this on page 25, with the following sample fragment:-<\/p>\n<p style=\"padding-left: 30px;\">@Inject<br \/>\nvoid initServices(@Any Instance&lt;Service&gt; services) {<br \/>\n\u00a0\u00a0\u00a0 for (Service service: services) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 service.init();<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing plugin architectures with IOC, it is useful to be able to iterate over the available beans of a given type which are known to the IO container. In CDI, this is easy to do, but it is not obvious how from the Weld documentation unless you do a bit of digging and searching. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[150],"tags":[110,181,16,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1364"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1364"}],"version-history":[{"count":7,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1364\/revisions"}],"predecessor-version":[{"id":1371,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1364\/revisions\/1371"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}