简体   繁体   中英

how to auto inject multiple beans into an ArrayList property with spring and its annotation

eg i have an array list property in my Action.

    private ArrayList<SitesBusiness> businesses;

and SitesBusiness is nothing but an interface, and this property is intended to contain all beans which implements SitesBusiness.

the question is that i want to use spring annotation mechanism to auto inject all the beans that implements SitesBusiness into businesses.

Any one can help? Many thanks.

i tried to use AutoWired on this method to do the job

    @Autowired
    public void addBusiness(SitesBusiness business) {
        System.out.println("SitesAction, addBusiness.DI1210, business.identifier: " + business.getIdentifier());
        for (int i = 0; i < this.businesses.size(); ++i) {
            if (GlobalMethods.getInstance().checkEqual(this.businesses.get(i), business) || GlobalMethods.getInstance().checkEqual(this.businesses.get(i).getIdentifier(), business.getIdentifier())) {
                return;
            }
        }
        this.businesses.add(business);
     }

Unfortunately, this is what i got:

     expected single matching bean but found 2: accountBusiness,diaryBusiness

This code should work:

@Autowired
private List<SitesBusiness> businesses;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM