The benefits of using the static factory method to create a bean do not necessarily come from Spring, which is just an IoC container.
Some suggestions Effective Java Idiom # 1, which provides a static factory, provides the following advantages over creating objects from the constructor:
- Gives your methods a more expressive name than a constructor.
- You can skip creating the actual object and provide a proxy.
- You can return a subtype of the return type of the method.
I find the biggest advantage of this idiom in the name of methods with similar signatures.
for example, if you have:
Person { String name; String[] booksAuthored;
Then you can create instances by calling them:
Person joshTheProgrammer = Person.createByName("Joshua Bloch");
or
Person joshTheAuthor = Person.createByBookName("Effective Java");
you cannot do this when using constructors, since you can only have one constructor that accepts a string.
source share