Search code examples
springpojospring-bean

How to define a property of type List<E> in a Spring Bean?


I work with Hybris and in the beans.xml file we can define POJOs to be used in projects.

I want to know how can I define a POJO in Spring with a property of type List where E should be another type I define in my beans.xml.

For example, I want to define a POJO like this:

public class MyPojo{
   private String someProperty;

   public String getSomeProperty(){
      return someProperty;
   }
   public void setSomeProperty(String someProperty){
      this.someProperty = someProperty;
   }
}

And another POJO that will contain a list of MyPojo:

public class MyPojoListHolder{
    private List<MyPojo> myPojoList;

   public List<MyPojo> getMyPojoList(){
      return myPojoList;
   }
   public void setMyPojoList(String myPojoList){
      this.myPojoList= myPojoList;
   }
}

MyPojo would be defined in my beans.xml as follows:

<bean class="my.package.MyPojo">
    <property name="someProperty" type="java.lang.String"></property>
</bean>

I can define MyPojoListHolder like this:

<bean class="my.package.MyPojoListHolder">
    <property name="myPojoList" type="java.util.List"></property>
</bean>

But that creates a class with myPojoList defined as a List object, but I'd like it to be defined as List.

How can I achieve this?


Solution

  • You can do, for example, something like:

    <property name="genders" type="java.util.List&lt;com.your.package.data.GenderData>"/>
    

    In your example, you would end up with

    <bean class="my.package.MyPojoListHolder">
      <property name="myPojoList" type="java.util.List&lt;my.package.MyPojo>"></property>
    </bean>