• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating beans by invoking a static factory method

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. I see. Thanks for the find Mark.
 
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic