Results 1 to 3 of 3

Thread: Optionally adding objects to list in bean file

  1. #1

    Default Optionally adding objects to list in bean file

    Hi, I have a bean definition file that has a bean that takes a list of items, so for example:

    Code:
      <bean id="MyBean" class="my.Class">
          <constructor-arg index="0">
              <list>
                  <ref bean="RequiredBean"/>
                  <ref bean="OptionalBean"/>
              </list>
          </constructor-arg>
      </bean>
    What happens is that I have a property (which is accessible to this bean file) that gets set, and if that property is true, I'd like to add the OptionalBean to the list, and if that property is false, I do not want to add that bean to the list. Is it possible to do something like this?

    Thanks,
    Jeff

  2. #2
    Join Date
    Sep 2005
    Posts
    6

    Default

    You can writed the owner FactoryBean for list.for example:

    <bean id="MyBean" class="my.Class">
    <constructor-arg index="0">
    <property name="customeBeanList">
    <ref bean="customeBeanList" />
    </property>
    </constructor-arg>
    </bean>
    <bean id="customeBeanList" class="my.custome.ListFactoryBean">
    <property name="isBoolean" value="true"/>
    <property name="requiredBean">
    <ref bean="RequiredBean" />
    </property>
    <property name="optionalBean">
    <ref bean="OptionalBean" />
    </property>
    </bean>

    public class ListFactoryBeanextends AbstractFactoryBean {
    private boolean isBoolean;
    private RequiredBean requiredBean;
    private OptionalBean optionalBean;
    public void setBoolean(boolean isBoolean){
    this.isBoolean = isBoolean;
    }
    public void setRequiedBean(RequiredBean requiredBean){
    this.requiredBean = requiredBean;
    }
    public void setOptionalBean (OptionalBean optionalBean){
    this.optionalBean= optionalBean;
    }
    public Class getObjectType() {
    return List.class;
    }

    protected Object createInstance() {
    List resultList = new ArrayList();
    resultList.add(requiredBean);
    if(isBoolean){
    resultList.add(optionalBean);
    }
    return resultList ;
    }
    }

    Hope to help your!
    David.liu

  3. #3

    Default Re: Optionall adding objects to list in bean file

    David,

    Thanks for that suggestion!

    Jeff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •