Is there any way in Spring for autowire all the dependencies of a given type?

I use annotation-based wiring (i.e. @Configurable(autowire=Autowire.BY_TYPE) ) for this class, and I would like to associate all the beans of this type with it as a list:

application context:

 <beans> <bean class="com.my.class.FirstConfigurer"/> <bean class="com.my.class.SecondConfigurer"/> </beans> 

for auto connection:

 @Configurable(autowire=Autowire.BY_TYPE) public class Target { ... public void setConfigurers(List<Configurer> configurers) { ... } } 

All dependencies implement the common Configurer interface.

Is there a way to make this work to have all the dependencies of the type bundled together in the collection and enter where necessary, or should I define a <list> in XML or something else?

+8
java spring spring-annotations
source share
2 answers

Yes,

 @Inject private List<Configurer> configurers; 

works, and you get a list of all beans that implement the interface. (several changes - @Inject or @Autowired , field, installation or installation of the constructor - everything works)

+7
source share

This should work:

 @Configurable(autowire=Autowire.BY_TYPE) public class Target { @Autowired public void setConfigurers(List<Configurer> configurers) { ... } } 

This is described in the section of section 3.9.2 of the Spring manual :

You can also provide all beans of a specific type from ApplicationContext by adding annotation to a field or method that expects an array of this type [...] The same applies to typed collections.

+2
source share

All Articles