2

Let's assume I have an Account class, that has a instance variable of Person class.

public class Account {
  private String accountId;
  private Person person;

  // getters and setters
}


public class Person {
  private String name;

  // getters and setters
}

Now If a form POST body contains the following params:

account.accountId=123&account.person.name=

Spring will instantiate a new Person even though all of its properties are empty (or null, cause I am using a @ControllerAdvice with @InitBinder, where I am setting StringTrimmerEditor for String). Is there a way to instruct Spring not to create the Person instance then?

Spase Markovski
  • 653
  • 1
  • 8
  • 22

1 Answers1

0

Hmm, I think not, because it is dependency injection principle to inject objects into objects. and loosely coupling of objects. If you want to have not create Person object please not provide setter for it. But I am not sure if spring would like to allow on it using annotations.

RMachnik
  • 3,598
  • 1
  • 34
  • 51