Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating beans by invoking a static factory method

 
Suhas Wadadekar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following code.

1. ProductCreator class that holds the static factory method to create a bean


2. bean declarations in the config file



As can be seen from the config file, while bean creation time, factory method from the class is invoked creating a bean based on constructor args. All fine and understood how it works.

The question is: Why? What is the purpose of doing it this way? Wasn't the whole point of IoC to decouple the components and get rid of new someClass() statements in code?

Comments welcome. I know I might be wrong/uninformed, but I seek enlightenment

Suhas.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I don't think that would work. The parameter to your factory-method is not a constructor-arg. The method is not a constructor.

Your factory-method method cannot take parameters.

Second, sometimes you will have legacy code that you can't change/touch and is written like this, so what do you do? You can't create an instance on your own and you have to call the method to create the object. So there is a configuration for those issues.

Mark

 
Suhas Wadadekar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, I did test this code, and it works!! The funny thing is what you pointed out. I am supplying values to the static factory method via constructor-arg tag. Take that! I like to call it Spring Magic. I'll try to look it up more.

The second point you made makes sense. Legacy code would need something in Spring to accomodate it. Otherwise, I think there should be a good reason to use a static factory method for bean instantiation, which Spring normally takes care for you anyways if you specify it in the config file.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there more code to your class that you didn't post. Like a Constructor on it??

Mark
 
Suhas Wadadekar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No code was left out. That is all. Of course I did not post the Product, Battery and Disc classes, but that does change any behavior related to static factory method.
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote: Your factory-method method cannot take parameters.


From ch. 3 of the official Spring reference manual:

Note that arguments to the static factory method are supplied via constructor-arg elements, exactly the same as if a constructor had actually been used.
 
Suhas Wadadekar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. I see. Thanks for the find Mark.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic