I’ve an interface BaseDao and other entity specific Dao interfaces which extend BaseDao.
public interface BaseDao<T extends RandomClass> {
// some methods
}
public interface FooDao extends BaseDao<RandomClassImpl1> {
// some methods
}
@Lazy
@Component(“fooDao”)
public class FooDaoImpl implements FooDao {
}
public interface BarDaoextends BaseDao<RandomClassImpl2> {
// some methods
}
@Lazy
@Component(“barDao”)
public class BarDaoImpl implements BarDao {
}
I also created a config, where I created a bean which returns a Map where dao component name is key and dao component is value.
@Lazy
@Configuration
@Import({BarDaoImpl.class, FooDaoImpl.class })
public class DaoRepositoryConfig {
@Bean(DAO_REPOSITORY)
Map<String, BaseDao> DaoRepository(
@Lazy @Qualifier(“fooDao”)
BaseDao<RandomClassImpl1> foo,
@Lazy @Qualifier(“barDao”)
BaseDao<RandomClassImpl2> bar
) {
Map<String, BaseDao> daoRepository = new HashMap<>();
daoRepository.put(“fooDao”, foo);
daoRepository.put(“barDao”, bar);
return daoRepository;
}
}
When I load this bean, I get a map of Dao names and dynamic proxy of corresponding implementation that I’ve imported in the config.
But when I try to type cast the proxy to immediate parent interface, it fails with ClassCastException.
(FooDao) daoRepo.get(“fooDao”) [ Here daoRepo.get(“fooDao”), returns proxy of FooDaoImpl which I could see in my debugger, but when I try to cast it to FooDao, ClassCast exception is thrown.