Announcement Announcement Module
No announcement yet.
How to configure a list of undetermined length Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to configure a list of undetermined length


    I am new to Spring and am having trouble understanding how to do the following scenario. I have googled for this extensively and cannot seem to find an answer.

    Here is the scenario:

    Let's say you have a bean with a List property. I have created a Book object with a title property which is a String and an authors property which is a List of Strings.
    import java.util.List;
    public class Book {
        private String title;
        private List<String> authors;
        public void setTitle(String title) { this.title = title; }
        public void setAuthors(List<String> authors) { this.authors = authors; }
        public String getTitle() { return title; }
        public List<String> getAuthors() { return authors; }
    For my test, I ask the user to enter a title, followed by an author. As long as the user keeps entering authors, it adds an author to the list.
    package test;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    import java.util.Properties;
    public class Main {
        public static void main(String[] args) {
            PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
            Properties properties = new Properties();
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            do {
                int authCt = 1;
                System.out.println("Enter the title of the book(Press Enter to cancel)");
                String answer = System.console().readLine();
                if(!answer.isEmpty()) {
                    properties.setProperty("book.title", answer);
                } else {
                do {
                    System.out.println("Enter an author's name(Press Enter to cancel)");
                    answer = System.console().readLine();
                    if(!answer.isEmpty()) {
                        properties.setProperty("" + authCt++, answer);
                    } else {
                } while(true);
                Book book = (Book)context.getBean("book");
                System.out.println("Title: " + book.getTitle());
                for(String author: book.getAuthors()) {
                    System.out.println("Author: " + author);
            } while(true);
    Below is my config file. The only way I have found to configure a list via xml is to have a list of values "hard-coded" either into the xml file itself, a properties file, or to set the properties specified in the xml file in the java code.
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
           xsi:schemaLocation="" default-lazy-init="true">
        <bean id="book" class="">
            <property name="title" value="${book.title}" />
            <property name="authors">
    This code will work as long as there are two authors entered for the book. If only one author is entered, a BeanDefinitionStoreException is thrown as the "book.author2" property is not set. If more than two authors are entered for the book, only the first two are stored in the Book object because the config file only allowed for two authors.

    My question is, how do you configure via xml (or annotations if it cannot be done with xml) a list property (or constructor arg) that you know will have a variable number of elements in it?

    I hope this isn't too elementary a question and that I haven't somehow missed a good answer to this in my googling, but I just can't seem to find the solution to this.

    Thanks in advance for your time.

  • #2
    From what I understand is that by dynamically adding properties to the configurer you want Spring to 'detect' this with hardwired configuration.
    I don't see how that's going to work.

    Why not get the bean without the authors initialized and feed it authors when the user does so? Or... don't specify a list of values in the xml-file, rather refer to a method that will return the list of entered authors?

    The latter; I haven't used it myself so I don't know if it'll work for you:
    <bean id="...">
    <lookup-method name=".." ... />


    • #3
      If I see that correctly, Book and Author are really domain objects. These are dynamic in nature and supposed to be altered in process of running your application. In fact, Spring is not intended to manage domain objects. If you want to store these somewhere then a database would be a better place to do so.
      The benefit of Spring is wiring together the static parts of your application (ie. the parts that "process" and not the parts that "are being processed").

      Hope that helps,


      • #4
        Thanks for the responses. It sounds like I am trying to use Spring for a purpose for which it is not intended.

        The part about Spring that was bugging me is the static nature of the beans that it manages. But, if it is intended to manage the static parts of an application, as you said the parts that "process", and not the dynamic objects that are being processed, it makes more sense. I will keep that in mind.