|
|
I have a standalone app that currently has a dao with a static property for the database connection. I want to switch over to spring/jdbc, but want to do it in pieces. I understand the set up of the DAO, but how do I make it available to other objects? My current thinking is to create a static property again to hold the application context or the DAO.....is there a better way with Spring?
Thanks
Hi jelgolfnut,
Ideally, you would use dependency injection to give other objects that depend on the DAO the appropriate class. However, if you wanted to do it little by little, you could do the following:
1) Write the DAO interface
2) Write the DAO implementation using Spring JDBC
3) Write the ApplicationContext file with the DAO and connection information
4) In classes that require the DAO, use the SingletonBeanFactoryLocator class to obtain the BeanFactory (and be sure it is only instantiated once) and then obtain the DAO. See the sample code in the Javadoc for the SingletonBeanFactoryLocator class.
Again, this is not recommended as you are using dependency pull instead of dependency injection, but if you just want to quickly see your existing code work with the Spring-managed DAO, you can give it a try.
Hope this helps!
-Arthur Loder |
|