When to use the ElementFactory method in Spring

I am using springs AutoPopulateList using the following METHOD 1

 passports = new AutoPopulatingList<Passport>(Passport.class); 

method2 as well as first creating a Passportfactory

public class PassportFactory implements AutoPopulatingList.ElementFactory {
  private Person person;

  public PassportFactory(Person person) {
     this.person = person;
  }

  public Object createElement(int index) {
    Passport passport = new Passport();
    passport.setPerson(person);
    return passport;
  }
}

and then using this

List<Passport> passports = new AutoPopulatingList(new PassportFactory(this));

Now both codes work, but I don’t know what the difference is between the two and how the second code will be useful because it is copied from the Internet. Can someone explain the difference to me

+3
source share
1 answer

If you use:

passports = new AutoPopulatingList<Passport>(Passport.class);Spring will use ReflectiveElementFactory<E>to create elements.

This means, for example, that list items must have a none parameter constructor.

, ElementFactory. - , , person , .

+3

All Articles