• 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

Spring factory not creating new objects

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a service factory single bean and it should return me a new instance of objects but its returning only the same object again and again. I knwo it make sense if you have a singleton factory object whose properties are set at constructor, but how can i make this to generate a new object everytime. Please let me know

<bean id="serviceFactory"
class="com.edmunds.impl.ServiceFactory"
factory-method="createInstance" singleton="true">
<constructor-arg ref="dwHelper"/>
<constructor-arg ref="dartHelper"/>
</bean>

<bean id="dwHelper"
class="com.edmunds.impl.DWForecastHelper" singleton="false">
</bean>


<bean id="dartHelper"
class="com.edmunds.impl.DartForecastHelper" singleton="false">
</bean>


Here is my Service Factory class

package com.edmunds.impl;

import com.edmunds.IForecastHelper;

import java.util.Date;

/**
* Created by IntelliJ IDEA.
* User: rnizamuddin
* Date: Nov 19, 2008
* Time: 1:54:48 PM
* To change this template use File | Settings | File Templates.
*/
public class ServiceFactory {

private IForecastHelper dwForecastHelper;
private IForecastHelper dartForecastHelper;
private static ServiceFactory me = null;

private ServiceFactory() {
init();
}

// a static factory method; the arguments to this method can be
// considered the dependencies of the bean that is returned,
// regardless of how those arguments are actually used.

public static ServiceFactory createInstance (DWForecastHelper dw,
DartForecastHelper dart) {
if(me == null) {
me = new ServiceFactory();
me.setDartForecastHelper(dart);
me.setDwForecastHelper(dw);
}
return me;
}


public static ServiceFactory createInstance(){
if (me == null){
me = new ServiceFactory();
}
return me;
}

public static ServiceFactory getInstance(){
return me;
}

public void init() {

}



public IForecastHelper createForecastHelper(Date endDate) {
return getDartForecastHelper();
}
public void setDwForecastHelper(IForecastHelper dwForecastHelper) {
this.dwForecastHelper = dwForecastHelper;
}

public IForecastHelper getDwForecastHelper() {
return dwForecastHelper;
}

public void setDartForecastHelper(IForecastHelper dartForecastHelper) {
this.dartForecastHelper = dartForecastHelper;
}

public IForecastHelper getDartForecastHelper() {
return dartForecastHelper;
}
}
 
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 "factory-method" in the bean tag, is only to tell SPring to create the single instance of the Factory, call that method. It will only call it once when you create your ApplicationContext. This is because the default scope of a bean is "singleton", which should be all you need. Now if you want a new instance everytime you call getBean on the application context, you have two choices.

1) change the scope to "prototype" or
2) have you Factory implement FactoryBean and either change the name of the getInstance method to getObject, or just implement the getObject method to call your getInstance method.

Mark
 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the update.
Hope i got you right. But still i want furthur explanantion
My serviceFactory class is a singleton which is fine thats what i want, and also understand that both the helper class are initialized when the the servicefactory singleton bean is created once in the constructor. and the same instance of those helper classes are returned each time. What i wanted was is there a way i can configure the service factory such that each time i say createForecastHelper i get a new bean and it should not return the same helper again and again, i hope i am right, if i put in pure object form it will be like a service factory will have a method called as

public IForecastHelper createForecastHelper(Date endDate){
if (endDate.after(new java.util.Date()){
return new DWForecastHelper();
} else{
return new DartForecastHelper();
}
)

I wanted to have a conventional singleton factory which can create enw instance of objects depending on the date i gave. how is that possible with spring.

Hope i am making sense.

Thanks
Rashid
 
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
The options are in my first post above.

Mark
 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i am not putting my question correct, please bear with me.
Lets step back and do some real factory example.
i have a singleton Factory class which returns me back a concrete implementaion of Shape interface

So for example i have an interface

public interface Shape(){
public int getArea();
}

Now i have threee concrete classes Circle, Rectangle and Square all implement Shape interface

public class Circle implements Shape{
public int getArea(){
System.out.println("circle area is " + 5);
}
}

public class Rectangle implements Shape{
public int getArea(){
System.out.println("Rectangle area is " + 5);
}
}

public class Square implements Shape{
public int getArea(){
System.out.println("Square area is " + 5);
}
}


Now here my Factory.java will be singleton which will return instance of concrete class in teh factory method

public Class Factory{
//a private constrcutor and a static variable to make the class singleton

public Shape getShape(int i){
if (i ==1){ //return circle
return new Circle();
}else if (i ==2){ //return square
return new Square();
}else { //return rectangle by default
return new Rectangle();
}
}

}


As you see in my above factory getShape method , depending on teh parameter it return the concrete object which implements Shape interface at runtime. i want the same thing with the ServiceFactory i had before. The problem with Spring is that you wire and initiallize everyting before hand and the factory is not able to provide based on a parameter at runtime. Could you please provide how would you implement the Area interace and the concrete Circle, Rectangle and Square factory pattern in spring.

I will really appreciate it.

Thanks
Rashid
 
Darvesh Niz
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any advice from anybody
Please...
Rashid
 
reply
    Bookmark Topic Watch Topic
  • New Topic