• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

GOF Singelton class in Spring 3

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to built an GOF type (not spring DI singleton) Singleton class in spring.



As, it is clear that to access the bean "userAuthenticationService", i need to fetch it from application context, hence i need to configure PrivilegesMgr in application context
After some R&D, i got the way




My problem is
Singleton class, traditionally called as PrivilegesMgr.getInstance() and not privilegesMgr.getInstance()

when, try to do PrivilegesMgr.getInstance(), i get null pointer, which is logical, as their is no bean available.
Any suggestion ?
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our Spring forum.
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any update guys?
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GOF = Gang of four
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Singleton class, traditionally called as PrivilegesMgr.getInstance() and not privilegesMgr.getInstance()



Yes, it's considered bad form to call a static method through it's class rather than an object reference, but there is really no difference in Java - it works either way. There's also no need to call "getInstance()" explicitly in your code - Spring has already called "getInstance()" for you - you can just call it as:



when, try to do PrivilegesMgr.getInstance(), i get null pointer, which is logical, as their is no bean available.



Why do you think there is no bean available? The code you posted looked OK - are you sure you're loading the beans XML configuration containing your "privilegesMgr" bean? Can you post relevant sections from your web.xml deployment descriptor?
 
reply
    Bookmark Topic Watch Topic
  • New Topic