Currently we have an application who use spring who support mysql.
Some people prefer to use Oracle.
So i search a way with spring to have an abstract factory with a factory for every database and each one have a dao.
How to put the glue between all this component?
How the component know the datasource who need to be used?
Is there some good pratice with spring to do this?
Currently we have an application who use spring who support mysql. Some people prefer
Share
Not clear what exactly is your problem, but Spring profiles are answer to all of them. First you need to define two
DataSources for each supported database:Note the
profileattribute. Actually you will probably get away with simply parametrizing one data source yo use different JDBC URL and driver, but doesn’t matter.Now you define two versions of each DAO: one for Oracle and one for MySQL:
As you can see you have two beans defined implementing the same interface. If you do it without profiles and then autowire them:
Spring startup will fail due to unresolved dependency. But if you enable one of the profiles (either
mysqlororacle) Spring will only instantiate and create bean for matching profile.