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:
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.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; } }
Main.java:
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.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); } }
beans.xml:
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.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>
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


Reply With Quote
