I assume this is a JAR application? You only call ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spconfig.xml"); one time thats it. Since you are calling it multiple times you are initializing those beans multiple times. Once you have the context you typically call getBean on whatever spring managed bean kicks things off. After that calling getBean again should not be required as all your classes will be spring managed and have the dependencies they need. Also consider registering a shutdown hook so that your DS gets cleaned up when you exit the application.
Then you should not be creating a new application context at all. That is done by the container using the stuff you should have registered in your web.xml. Also you should not need to use the connection at all let spring manage that stuff. Use the JDBC template
Don't use old versions of Spring. Spring 2.5 is very old. Use a current version to learn with.
I suggest you start by downloading STS.
In STS select File > New > Spring project - when you get there, you will see bunch of options under "Templates" section. One of them is Spring MVC Project. Select that and follow the directions.
You should now be able to run a very simple Spring MVC web-application. Now you just need to edit the spring context xml file to add you datasource bean. Have Spring inject the datasource into your repository bean constructor (using @Autowired) where you can use it to construct a JDBC template for accessing your database.
Joined: May 16, 2013
Thank you again bill you told that about STS ...
Can you please tell me this..
If suppose i use Spring MVC 2.5 i have to inject the datasource object into jdbctemplate for each and every class right ??
And if i use Spring MVC 3 and the annotation @Autowired takes care of injecting the datasource object into all the related classes right ??