This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Spring and the fly likes Single or multiple instances Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Frameworks » Spring
Reply Bookmark "Single or multiple instances" Watch "Single or multiple instances" New topic
Author

Single or multiple instances

nilu chan
Greenhorn

Joined: Aug 31, 2010
Posts: 15
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 ?



Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Single or multiple instances
 
Similar Threads
Spring sample applications - Need Help
spring portlet mvc + hibernateDaoSupport problem
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException:
Spring2.5.6+Tomcat7: HTTP Status 404: The requested resource () is not available
Bean Initialization fails... Why?