aspose file tools*
The moose likes Other Application Frameworks and the fly likes Spring factory not creating new objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring factory not creating new objects" Watch "Spring factory not creating new objects" New topic
Author

Spring factory not creating new objects

Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
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;
}
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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


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

Joined: May 12, 2008
Posts: 119
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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

The options are in my first post above.

Mark
Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
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

Joined: May 12, 2008
Posts: 119
Any advice from anybody
Please...
Rashid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring factory not creating new objects