Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 791009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:46:02+00:00 2026-05-14T21:46:02+00:00

Is there a way in Spring to create a collection, or array, of beans,

  • 0

Is there a way in Spring to create a collection, or array, of beans, based on a comma-separated list of classes. For example:

package mypackage;
public class Bla {
 private Set<MyBean> beans;
 public void setBeans(Set<MyBean> beans) {
  this.beans = beans;
 }
}

With the application context:

<bean id="bla" class="mypackage.Bla">
 <property name="beans">
  <set>
   <bean class="mypackage.Bean1, mypackage.Bean2" />
  </set>
 </property>
</bean>

Preferably the beans are all initialized and wired from the context, leaving the code as simplistic as possible, is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:46:02+00:00Added an answer on May 14, 2026 at 9:46 pm

    Use a combination of ApplicationContextAware and ApplicationListener:

    public class BeanInitializer implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {
    
        private ApplicationContext  context;
        private List<Class<?>>      beanClasses;
    
        public void onApplicationEvent(final ContextRefreshedEvent event) {
            final AutowireCapableBeanFactory beanFactory = this.context.getAutowireCapableBeanFactory();
            for (final Class<?> beanClass : this.beanClasses) {
                beanFactory.autowire(beanClass, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
            }
        }
    
        public void setApplicationContext(final ApplicationContext context) throws BeansException {
            this.context = context;
        }
    
        public void setBeanClasses(final List<Class<?>> beanClasses) {
            this.beanClasses = beanClasses;
        }
    
    }
    

    in your spring config, do this:

    <bean class="com.yourcompany.BeanInitializer">
    <property name="beanClasses">
        <list>
            <value>com.yourcompany.Type1</value>
            <value>com.yourcompany.Type2</value>
            <value>com.yourcompany.Type3</value>
        </list>
    </property>
    </bean>
    

    Edited: Actually, if you want comma separated, it will probably be more like this:

    <bean class="com.yourcompany.BeanInitializer">
    <property name="beanClasses" 
            value="com.yourcompany.Type1,com.yourcompany.Type2,com.yourcompany.Type3" />
    </bean>
    

    I don’t know if there is a built-in property editor that converts a comma delimited string to a list of classes but if not you can either create one yourself or change your setter method to accept a string and parse the string yourself

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most elegant way in Rails to create a comma-separated list inside
Is there any way to create a new NSString from a format string like
Is there a way to create a DOM object from the whole string, not
Is there an easy way to create a multiline string literal in C#? Here's
Is there any way to validate spring context xml files in eclipse? Features like:
Just wondering if there is a way in Spring to have a parent controller:
Is there a way to add the ActiveMQ component via javacode in Spring's applicationConfig
Is there any way to post large files (>150MB) to Dropbox service with Spring
Is there a way to obtain the post data itself? I know spring handles
Is there ANY way of monitoring primitive String declaration in NodeJS? For example, when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.