Skip to content

Instantly share code, notes, and snippets.

@asvignesh
Created December 11, 2021 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asvignesh/f92e6f0df7fb0881fe2e16ec43f6aff5 to your computer and use it in GitHub Desktop.
Save asvignesh/f92e6f0df7fb0881fe2e16ec43f6aff5 to your computer and use it in GitHub Desktop.
Factory
@Component
@Slf4j
public class AwsRequestHandlerDi {
private static final Map<ApplicationCommands, INimesaAwsJob> myServiceCache = new HashMap<>();
private final List<INimesaAwsJob> handlers;
@Autowired
private AwsRequestHandlerDi(List<INimesaAwsJob> handlers) {
this.handlers = handlers;
}
public static INimesaAwsJob getService(String command) {
ApplicationCommands applicationCommands = ApplicationCommands
.valueOf(command);
INimesaAwsJob service = myServiceCache.get(applicationCommands);
if (null == service) {
throw new RuntimeException("Unknown service type: " + applicationCommands);
}
return service;
}
@PostConstruct
public void initMyServiceCache() {
for (INimesaAwsJob service : handlers) {
RequestHandler annotation = service.getClass().getAnnotation(RequestHandler.class);
ApplicationCommands[] commands = annotation.command();
for (ApplicationCommands command : commands) {
myServiceCache.put(command, service);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment