• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

spring singleton question

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create application context only one time.
 
gianluca gian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean that I should create only one appllication context calling the following only once???

ApplicationContext contextBusiness = new ClassPathXmlApplicationContext
("com/spring/serviceConfig.xml");

and then? what I should do to call the context again? put it in session?

good morning
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make such class and use it in any part of your application:
 
gianluca gian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much. Ciao
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic