wood burning stoves 2.0*
The moose likes Spring and the fly likes Creating Beans by invoking static factory method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Creating Beans by invoking static factory method" Watch "Creating Beans by invoking static factory method" New topic
Author

Creating Beans by invoking static factory method

PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
Hi while reading the book -Appress Spring Recipes- A problem solution Approach i came across the ways of creating beans using static factory method....

I will give the source code below for easy understanding

The abstract class Product



The subclass Dvd



The subclass Battery



The factory method implementor class ProductCreator


the beans.xml


the main class which creates the beans Main.java





My doubt is this guys if i comment the Application Context line still i get the correct output.

What is the use of the factory method and what happens when the ApplicationContext line is called?

What i mean is if i call ProductCreator.createProduct("aaa") i am going to get a bean so what is the use of the factory-method declared in the beans.xml file?

What does the factory-method in the beans.xml signify?

How does it affects my program?

Please help me here.........

Sorry if i have given the entire source code..


To err is human,
To forgive is not company policy
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

The reason why it works in your code when you comment out the App context stuff is because you aren't even using it in your last two lines. You are bypassing it by calling a static method in the Factory class directly.

So the code is to demonstrate that it works the same way.

factory-method states call this method instead of a constructor when requesting that bean.

so



In the creation of aaa bean, Spring will call the createProduct method of ProductCreator, passing in the constructor-arg of "aaa" to the createProduct method.
Spring will not create "aaa" via a constructor in ProductCreator.

Remember a bean tag means Spring is going to create an instance of your object for you. Either via a constructor method, or via some other means when a constructor isn't available, (in this case a static factory method).

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
saurabh banerjeejava
Greenhorn

Joined: Feb 06, 2012
Posts: 2
Going one step ahead in the example why both the product subclasses (Battery & Disc ) gets instantiated .


public class Disc extends Product {
private int capacity;
public Disc() {
super();
}
public Disc(String name, double price) {
super(name, price);
System.out.println("INVOKING DISC SUBCLASS");
}

public class Battery extends Product{
private boolean rechargeable;
public Battery() {
super();
}
public Battery(String name, double price) {
super(name, price);
System.out.println("INVOKING BATTERY SUBCLASS");
}
}

public class ProductCreator {
public static Product createProduct(String productId) {
if ("aaa".equals(productId)) {
return new Battery("AAA", 2.5);
} else if ("cdrw".equals(productId)) {
return new Disc("CD-RW", 1.5);
}
throw new IllegalArgumentException("Unknown product");
}
}

and in the main.java

public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Product aaa = (Product) context.getBean("aaa");
}


applicationContext.xml

<bean id="aaa" class="com.ProductCreatorFactory.ProductCreator"
factory-method="createProduct">
<constructor-arg value="aaa" />
</bean>
<bean id="cdrw" class="com.ProductCreatorFactory.ProductCreator"
factory-method="createProduct">
<constructor-arg value="cdrw" />
</bean>



OUTPUT :--
INVOKING BATTERY SUBCLASS
INVOKING DISC SUBCLASS

how both of them get instantiated .
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Please use the CODE tags when you post code so we can read the code.

Thanks

Mark
saurabh banerjeejava
Greenhorn

Joined: Feb 06, 2012
Posts: 2
Mark Spritzler wrote:Please use the CODE tags when you post code so we can read the code.

Thanks

Mark


Sorry , for the inconvinence , I am resending my problem . In the discussed product hirarchy example , when I execute , I get unexpected output (on adding sop (system print) in the contructor of Disc & Battery ) , could you please explain .












-----------------------------------------------

The OUTPUT I get is :--
INVOKING BATTERY SUBCLASS
INVOKING DISC SUBCLASS

My understanding is :- when context.getBean("aaa"); it should configures Battery.java since the constructor argument passed is aaa (<constructor-arg value="aaa" /> ) in public static Product createProduct(String productId) so BATTERY class should be instatiated but it instantiates bot BATTERY and DISC .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Beans by invoking static factory method