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 6543505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:21:18+00:00 2026-05-25T11:21:18+00:00

In spring, I want to inject values in list property dynamically. Is it possible?

  • 0

In spring, I want to inject values in list property dynamically. Is it possible?

For e.g. Instead of specifying value 1 three times, does there exist some property which can repeat this values multiple times based on some value.

<bean id='myBean' class-"com.foo.Xyz">
 <property name="myList">
    <value>1</value>
    <value>1</value>
    <value>1</value>
 </property>
</bean>

Please let me know if question is not clear.

  • 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-25T11:21:19+00:00Added an answer on May 25, 2026 at 11:21 am

    What about custom FactoryBean?

    public class RepeatingListFactoryBean implements FactoryBean<Object> {
    
        private final Object item;
        private final int count;
    
        public RepeatingListFactoryBean(Object item, int count) {
            this.item = item;
            this.count = count;
        }
    
        @Override
        public Object getObject() throws Exception
        {
            final ArrayList<Object> list = new ArrayList<Object>(count);
            for(int i = 0; i < count; ++i)
                list.add(item);
            return list;
        }
    
        @Override
        public Class<?> getObjectType() {
            return item.getClass();
        }
    
        @Override
        public boolean isSingleton() {
            return true;
        }
    }
    

    You can use it in the following way (however I haven’t tested it):

    <bean id="listFactory" class="RepeatingListFactoryBean">
      <constructor-arg value="1"/>  <!-- item -->
      <constructor-arg value="3"/>  <!-- count -->
    </bean>
    
    <bean id="myBean" class-"com.foo.Xyz">
      <property name="myList" ref="listFactory"/>
    </bean>
    

    Note that both count and an object that is to be repeated are declaratively configurable.

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

Sidebar

Related Questions

I have a bean that i want to inject with a named list using
I'm using Spring 3 and want to inject some dependencies into a class that
I want to inject a plain java object using Spring programmatically without using any
I want to inject a connection string into my repository but ideally, I want
I want to do something like the following in spring: <beans> ... <bean id=bean1
I want to declare two beans and instantiate them using Spring dependency injection? <bean
i want to call 2 views within one controller in spring with java. for
I want to use @PreAuthorize annotation on service methods with Spring Security. One of
I want to use both ContextLoaderListener (so that I can pass Spring Beans to
I want to specify a file system path as part of a Spring bean

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.