good morning,
I have a llittle doubt about spring singleton. I set up a little experiment to see how the spring singleton works, but I cannot see any resoults. I'm doing the next. On a
servlet I have the following code:
TestService testService= (TestService) // TestService is an interface
SpringFactory.getSISBean( "testService");
System.out.println("testSingleton - pippo: "+testService.getPippo());
testService.setPippo("CIAO");
req.getRequestDispatcher("JSPPageTest").forward(req, res);
where SpringFactory is:
public class SpringFactory {
public synchronized static Object getSISBean(
String nomeBean) {
ApplicationContext contextBusiness = new ClassPathXmlApplicationContext
("com/spring/serviceConfig.xml");
return contextBusiness.getBean(nomeBean);
}
}
in my spring xml bean configuration (serviceConfig.xml) file I have:
<bean id="testService" class="com.service.impl.TestService" >
<constructor-arg ref="someDao"/>
</bean>
After the
jsp is displayed, in order to call the Servlet again I press
F. On the log I have:
-------------------------------- First time
[14/08/09 16.42.41:547 CEST] 0000003e ClassPathXmlA I Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@beeb0c1:
display name
[org.springframework.context.support.ClassPathXmlApplicationContext@beeb0c1];
startup date [Fri Aug 14 16:42:41 CEST 2009]; root of context
hierarchy
[14/08/09 16.42.41:969 CEST] 0000003e XmlBeanDefini I Loading XML
bean definitions from class path resource [com/spring/
serviceConfig.xml]
[14/08/09 16.42.42:312 CEST] 0000003e ClassPathXmlA I Bean factory
for application context
[org.springframework.context.support.ClassPathXmlApplicationContext@beeb0c1]:
org.springframework.beans.factory.support.DefaultListableBeanFactory@369c30c0
[14/08/09 16.42.44:844 CEST] 0000003e DefaultListab I Pre-
instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@369c30c0:
defining beans [testService]; root of factory hierarchy
[14/08/09 16.42.56:391 CEST] 0000003e SystemOut O testSingleton -
pippo: null
----------------------------- Second time
[14/08/09 16.42.59:391 CEST] 0000003e ServletWrappe A SRVE0242I:
[JSPPageTest]: Inizializzazione corretta.
[14/08/09 16.43.11:766 CEST] 0000003b ClassPathXmlA I Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@4af830e4:
display name
[org.springframework.context.support.ClassPathXmlApplicationContext@4af830e4];
startup date [Fri Aug 14 16:43:11 CEST 2009]; root of context
hierarchy
[14/08/09 16.43.11:781 CEST] 0000003b XmlBeanDefini I Loading XML
bean definitions from class path resource [com/spring/
serviceConfig.xml]
[14/08/09 16.43.11:969 CEST] 0000003b ClassPathXmlA I Bean factory
for application context
[org.springframework.context.support.ClassPathXmlApplicationContext@4af830e4]:
org.springframework.beans.factory.support.DefaultListableBeanFactory@580470e4
[14/08/09 16.43.12:031 CEST] 0000003b DefaultListab I Pre-
instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@580470e4:
defining beans [testService]; root of factory hierarchy
[14/08/09 16.43.16:656 CEST] 0000003b SystemOut O testSingleton -
pippo: null
as you can see pippo is null the first time (what it is suppost to), and is null the second time. Isnt it a strange thing to have null the second time since the TestService is a singleton???