File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Creating beans by invoking a static factory method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Creating beans by invoking a static factory method" Watch "Creating beans by invoking a static factory method" New topic
Author

Creating beans by invoking a static factory method

Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

Is there more code to your class that you didn't post. Like a Constructor on it??

Mark
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
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

Joined: Oct 08, 2009
Posts: 276
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.


OCJP 6 (93%)
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
Oh. I see. Thanks for the find Mark.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating beans by invoking a static factory method
 
Similar Threads
Factory Vs Abstract Factory Pattern
Creating Beans by invoking static factory method
Class cast exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name...
Spring factory not creating new objects