Announcement Announcement Module
Collapse
No announcement yet.
Optionally adding objects to list in bean file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      Re: Optionall adding objects to list in bean file

      David,

      Thanks for that suggestion!

      Jeff

      Comment

      Working...
      X