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

  • How to configure a list of undetermined length

    Hello

    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.
    Book.java:
    Code:
    package spring.book;
    
    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.
    Main.java:
    Code:
    package test;
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    import java.util.Properties;
    import spring.book.Book;
    
    public class Main {
    
        public static void main(String[] args) {
    
            PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
            Properties properties = new Properties();
            configurer.setProperties(properties);
    
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            context.addBeanFactoryPostProcessor(configurer);
            context.registerShutdownHook();
    
            do {
    
                properties.clear();
                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 {
                    break;
                }
    
                do {
                    System.out.println("Enter an author's name(Press Enter to cancel)");
                    answer = System.console().readLine();
                    if(!answer.isEmpty()) {
                        properties.setProperty("book.author" + authCt++, answer);
                    } else {
                        break;
                    }
                } while(true);
                context.refresh();
                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.
    beans.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" default-lazy-init="true">
    
        <bean id="book" class="spring.book.Book">
            <property name="title" value="${book.title}" />
            <property name="authors">
                <list>
                    <value>${book.author1}</value>
                    <value>${book.author2}</value>
                </list>
            </property>
        </bean>
      
      </beans>
    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.
    darmstrong

  • #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=".." ... />
    </bean>

    Comment


    • #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,
      Andreas

      Comment


      • #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.

        Thanks,
        darmstrong

        Comment

        Working...
        X