Skip to content

Instantly share code, notes, and snippets.

@DattatreyaReddy
Last active November 25, 2025 11:52
Show Gist options
  • Select an option

  • Save DattatreyaReddy/250e7a1f8de77026f7527e5ef16dfeae to your computer and use it in GitHub Desktop.

Select an option

Save DattatreyaReddy/250e7a1f8de77026f7527e5ef16dfeae to your computer and use it in GitHub Desktop.
DocumentGenerator Annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Service
public @interface DocumentGenerator {
DocumentGenerationEntityType value();
}
package com.assist.core.factory;
import com.assist.commons.annotations.DocumentGenerator;
import com.assist.commons.documentGeneration.DocumentInfoDto;
import com.assist.commons.documentGeneration.constants.DocumentGenerationEntityType;
import com.assist.commons.documentGeneration.service.EntityDocumentGenerationService;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang.NotImplementedException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class DocumentGeneratorBeanFactory implements ApplicationContextAware, InitializingBean {
private final Map<DocumentGenerationEntityType, EntityDocumentGenerationService> typeBeanMap =
new HashMap<>();
@Autowired
private ListableBeanFactory listableBeanFactory;
private ApplicationContext applicationContext;
public <EntityInfoDto extends DocumentInfoDto, EntityDto> EntityDocumentGenerationService<EntityInfoDto, EntityDto> getBean(
DocumentGenerationEntityType entityType) {
return typeBeanMap.get(entityType);
}
@Override
public void afterPropertiesSet() throws Exception {
initializeTypeBeanMap();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
//Method to initialize or create a map to store entityType and corresponding bean of entityType
private void initializeTypeBeanMap() {
Map<String, Object> beansWithAnnotation =
applicationContext.getBeansWithAnnotation(DocumentGenerator.class);
for (String beanName : beansWithAnnotation.keySet()) {
EntityDocumentGenerationService documentGenerationService =
listableBeanFactory.getBean(beanName, EntityDocumentGenerationService.class);
DocumentGenerator beanHandler =
listableBeanFactory.findAnnotationOnBean(beanName, DocumentGenerator.class);
if (Objects.isNull(beanHandler)) {
throw new NotImplementedException(beanName + " Bean not found!");
}
DocumentGenerationEntityType entityType = beanHandler.value();
typeBeanMap.put(entityType, documentGenerationService);
}
}
}
/// factory usage
private <EntityInfo extends DocumentInfoDto, EntityDto>
EntityDocumentGenerationService<EntityInfo, EntityDto> getEntityDocumentGenerationService(
DocumentGenerationEntityType entityType) {
try {
return documentGeneratorBeanFactory.getBean(entityType);
} catch (Exception e) {
throw new IllegalStateException(entityType + " PDF Implementation Not Found");
}
}
private <EntityInfo extends DocumentInfoDto, EntityDto>
EntityDocumentCreationService<EntityInfo, EntityDto> getEntityDocumentCreationService(
DocumentGenerationEntityType entityType) {
EntityDocumentGenerationService<EntityInfo, EntityDto> generationService =
this.<EntityInfo, EntityDto>getEntityDocumentGenerationService(entityType);
if (!(generationService instanceof EntityDocumentCreationService)) {
throw new IllegalStateException(
"Can not create document for " + entityType.getDisplayName());
}
return (EntityDocumentCreationService<EntityInfo, EntityDto>) generationService;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment