• 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:

Single or multiple instances

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a spring web based application that has access to the database and I have the following class defined for accessing database.

MasterDao.java


I am using the following xml mapping for wiring the DAO class in my applicationContext.xml file
applicationContext.xml



I have the following questions related to my MasterDao class.
1) Will two instances of MasterDao class be created? One for showAllProductsController and one for addToCartController ?
2) Shouldn't all the methods defined in MasterDao class be static so that nobody can new an instance and call these methods ?



 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will two instances of MasterDao class be created? One for showAllProductsController and one for addToCartController ?


A single instance of MasterDAO will be created & injected in the controllers. The default scope for beans created by Spring is singleton.

Shouldn't all the methods defined in MasterDao class be static so that nobody can new an instance and call these methods ?


For beans created/manged by Spring the new construct is not supposed to be used. Spring creates the beans & manages the lifecycle of the beans, i:e manages dependency.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic