{"id":5103,"date":"2014-03-30T18:48:11","date_gmt":"2014-03-30T18:48:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/spring-list-beans-by-type-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:48:11","modified_gmt":"2014-03-30T18:48:11","slug":"spring-list-beans-by-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/spring-list-beans-by-type-collection-of-common-programming-errors\/","title":{"rendered":"Spring list beans by type-Collection of common programming errors"},"content":{"rendered":"<p>Yup, you can do this. The spring docs say:<\/p>\n<blockquote>\n<p>It is also possible to provide all beans of a particular type from the ApplicationContext by adding the annotation to a field or method that expects an array of that type.<\/p>\n<\/blockquote>\n<p>Note that it says you need to expect an array, not a list. This makes sense, because generic type erasure means a list may not work at runtime. However, take the following unit test, which works:<\/p>\n<pre><code>\n  \n\n    \n    \n    \n\n<\/code><\/pre>\n<p>and this unit test:<\/p>\n<pre><code>package test;\n\n@ContextConfiguration\n@RunWith(SpringJUnit4ClassRunner.class)\npublic class Test {\n\n    private @Autowired List beans;\n\n    @org.junit.Test\n    public void test() {\n        assertNotNull(beans);\n        assertEquals(2, beans.size());\n        for (TypeA bean : beans) {\n                assertTrue(bean instanceof TypeA);\n        }\n    }           \n\n    public static interface TypeA {}\n    public static class TypeB implements TypeA {}\n    public static class TypeC extends TypeB {}\n    public static class TypeD {}\n\n}\n<\/code><\/pre>\n<p>So officially, you&#8217;re supposed to autowire <code>TypeA[]<\/code>, not <code>List<\/code>, but the List works good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yup, you can do this. The spring docs say: It is also possible to provide all beans of a particular type from the ApplicationContext by adding the annotation to a field or method that expects an array of that type. Note that it says you need to expect an array, not a list. This makes [&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-5103","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5103","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=5103"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5103\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}