• 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 static factory method

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
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
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
Please use the CODE tags when you post code so we can read the code.

Thanks

Mark
 
saurabh banerjeejava
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic