开发者

When to use ElementFactory method in Spring

I am using springs AutoPopulateList using following METHOD 1

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

METHOD2 and also first creating 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 PassportFa开发者_如何学Cctory(this));

Now both codes are working but i don't know what is the difference between two and how second code will be helpful because copied from internet. Can someone explain me the difference


If you use:

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

This mean for example that your list elements must have a none parameter constructor.

But if you do not have such a constructor for your list elements, or need to build the elements in a special way, than you need to create your own ElementFactory. -- For example your second code, set the person field in every create list element, while the first one will leave this field empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜