lundi 12 juin 2017

Equivalent of @Configurable for CDI JavaEE 7

Currently I have a big problem with CDI, when I would like to create a new object by their annotaitons.

With reflections I got all classes taged with '@Connector'. After that I create for each class a new object. This works fine, but I would like to inject a ServiceHandler into the objects which try to inject it via '@Inject'. The Problem here is that CDI doesn't know these objects and will not be able to inject them. Currently I have a workaround to solve this problem, but this is not really pretty. I'm new to CDI and I'm looking for an equivalent of Spring's '@Configurable' annotation.

private Set<ApiDao> determineApiDaos() {
Set<Class<?>> classes = new Reflections("###packageName###").getTypesAnnotatedWith(Connector.class);
return FluentIterable.from(classes)
    .transform(CLASS_TO_API_DAO_FUNCTION)
    .filter(Predicates.notNull())
    .toSet(); 
}

private ApiDao instantiateApiDao(Class apiDao) {
try {
  ApiDao newApiDao = (ApiDao) apiDao.newInstance();
  newApiDao.setConfigurationService(configurationService); // Workaround inject service during creation
  return newApiDao;
} catch (Exception e) {
  LOG.error("Could not initialize Connector.", e);
  return null;
  }
}





Aucun commentaire:

Enregistrer un commentaire