I have a core module with a class that contains:
@Autowired
private BaseDao dao;
and few implementations of BaseDao interface:
class JdbcBaseDaoImpl implements BaseDao {...}
class HibernateBaseDaoImpl implements BaseDao {...}
And few modules which use that class from the core module (using maven).
But in the first module, I want to use JdbcBaseDaoImpl implementation in that field of core module and in the second module, to use HibernateBaseDaoImpl implementation.
How to do that? In other words, how to use class in core module with
@Autowired
@Qualifier("jdbcBaseDaoImpl")
private BaseDao dao;
in first module and
@Autowired
@Qualifier("hibernateBaseDaoImpl")
private BaseDao dao;
in second module?
The annotation is
@Qualifier("<name>"). See this for more details.BTW, the Java EE equivalent of this is
@Resource(name="<name>").If these modules are going to run in the same process space, then it is not possible to conditionally inject different implementations into the same variable in core module based on the code path. You can instead push the BaseDao reference into the dependent modules, inject the appropriate one for each and pass it as a reference to the core module’s method that need it.
If these modules are going to run in different process space, then you can configure the appropriate implementation for each module, in the process specific spring configuration file (This we do for separating the implementations for production and unit test stages).