2 Comments

Will it be better to use DI to replace registerHandler?

For example in Spring:

Use `@Component("Name")` to define handlers.

Then use `notificationHandlers = applicationContext.getBeansOfType(NotificationHandler.class)` to get the handler map when constructing the service object.

Finally use notificationHandlers.get(channel.getName()).send() to retrieve the handler object and call send() to send out the notification.

Expand full comment

There are different styles to implement the pattern. This post is talking about a general way. If you are using Spring boot, then DI definitely helps. Personally, I like to use enum as key and register the handler into a Map. The reason why I like this style:

1. Avoid typo in registering component name

2. I can have a central place to see and register my handlers

3. Decouple from Spring boot, I can control when the handler is available to use by registering to my Map manually

Expand full comment