Just small but complete test to prove my disbelief
Main class (A.java)
Code:
import org.springframework.beans.*;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class A {
public static void main(String[] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext("myContext.xml");
System.out.println("First fetch of B1 ["+ac.getBean("B1")+"]");
System.out.println("Second fetch of B1 ["+ac.getBean("B1")+"]");
}
}
Class for bean1 (B1.java)
Code:
public class B1 {
Object fBean2;
public void setBean2(Object bean2) {
fBean2= bean2;
System.out.println("Bean2 ["+System.identityHashCode(bean2)+"]");
}
}
Class for bean2 (B2.java)
Code:
public class B2 {
Object fBean3;
public void setBean3(Object bean3) {
fBean3= bean3;
System.out.println("Bean3 ["+System.identityHashCode(bean3)+"]");
}
}
and for bean3 (B3.java)
Code:
public class B3 {
}
and now context (myContext.xml)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="B1" class="B1" scope="prototype">
<property name="bean2" ref="B2"/>
</bean>
<bean id="B2" class="B2" scope="prototype">
<property name="bean3" ref="B3"/>
</bean>
<bean id="B3" class="B3" scope="prototype"/>
</beans>
and, just for completness, log4j.properties
Code:
log4j.rootLogger=INFO, console
#Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - <%m>%n
and now output
Code:
2007-07-25 15:25:03,453 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - <Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd5b: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd5b]; startup date [Wed Jul 25 15:25:03 CEST 2007]; root of context hierarchy>
2007-07-25 15:25:03,594 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [myContext.xml]>
2007-07-25 15:25:03,923 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - <Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd5b]: org.springframework.beans.factory.support.DefaultListableBeanFactory@a37368>
2007-07-25 15:25:03,954 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a37368: defining beans [B1,B2,B3]; root of factory hierarchy>
Bean3 [4116479]
Bean2 [29987161]
First fetch of B1 [B1@65a77f]
Bean3 [30911772]
Bean2 [10883428]
Second fetch of B1 [B1@bfc8e0]
So you see that all bean instances are different. So check your code and configuration once more - somewhere there is error.
Regards,
Oleksandr