Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowire reference beans into list by type

Tags:

java

spring

I have one class that has a list of objects of Daemon type.

class Xyz {         List<Daemon> daemons; } 

My spring configuration looks like this.

<bean id="xyz" class="package1.Xyz">    <property name="daemons" ref="daemonsList"> </bean>  <bean id="daemon1" class="package1.DaemonImpl1"/> <bean id="daemon2" class="package1.DaemonImpl2"/>  <bean id="daemonsList" class="java.util.ArrayList">         <constructor-arg>             <list>                 <ref bean="daemon1" />                       <ref bean="daemon2" />             </list>         </constructor-arg> </bean> 

Now instead of explicitly wiring each daemon implementation in list, is it possible to autowire all beans of type Daemon automatically in list. Problem I am trying to solve is, If someone creates a bean of new implementation of Daemon class and forgets to wire it into list.

I have seen this question somewhere on stackoverflow but not able to find that again. Apologies for it.

like image 576
RandomQuestion Avatar asked Sep 16 '11 14:09

RandomQuestion


People also ask

Does spring @autowired inject beans by name or by type?

if Spring encounters multiple beans with same type it checks field name. if it finds a bean with the name of the target field, it injects that bean into the field.

Can you Autowire by type?

This mode specifies autowiring by property type. Spring container looks at the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in the configuration file.

Can you Autowire by type when more than one bean?

3. Autowiring using property type. Allows a property to be autowired if exactly one bean of property type exists in the container. If more than one exists, it's a fatal exception is thrown, which indicates that you may not used byType autowiring for that bean.


1 Answers

It should work like this (remove the ArrayList bean from your XML):

public Class Xyz {          private List<Daemon> daemons;      @Autowired     public void setDaemons(List<Daemon> daemons){         this.daemons = daemons;     }  } 

I don't think there's a way to do this in XML.


See: 3.9.2. @Autowired and @Inject:

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:

public class MovieRecommender {    @Autowired   private MovieCatalog[] movieCatalogs;    // ... } 

The same applies for typed collections:

public class MovieRecommender {    private Set<MovieCatalog> movieCatalogs;    @Autowired   // or if you don't want a setter, annotate the field   public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {       this.movieCatalogs = movieCatalogs;   }    // ... } 

BTW, as of Spring 4.x, these lists can be ordered automatically using the @Ordered mechanism.

like image 161
Sean Patrick Floyd Avatar answered Oct 17 '22 23:10

Sean Patrick Floyd