Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GOF Singelton class in Spring 3

 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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 ?
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our Spring forum.
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any update guys?
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOF = Gang of four
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic