I want to draw UML diagram which represents my Spring boot application. I'm new in UML and don't know how to represent an interaction between two classes that uses other classes for it. I have a class that publishes spring events and a class which handles such events. Publisher example:
import org.springframework.context.ApplicationEventPublisher;
class Publisher {
ApplicationEventPublisher springPublisher;
public publishEvent() {
springPublisher.publishEvent(new SomeEvent());
}
}
Handler:
import org.springframework.context.event.EventListener;
class EventHandler {
@EventListener
public handleEvent(Event event) {
// some processing
}
}
So, when I publish some events via Spring ApplicationEventPublisher my handler handles events via method annotated with @EventListener. But it is not a direct calling of the method. How I can illustrate it on the UML diagram?
As qwerty_so rightly pointed out, in a class diagram, we do not represent interactions: we represent structure, such as classes, association between classes, and dependencies:
EventHandler
side, you have a dependency to Event
. Since it's an @EventListener
, Event
is probably an ApplicationEvent
Publisher
side, your code has a dependency to SomeEvent
. Since it's an ApplicationEventPublisher
, SomeEvent
is probably an ApplicationEvent
.You may therefore want to show a dependency to the common ApplicationEvent
:
I'm not a Spring expert, but I understand that behind the "spring magic" is the annotation which hides an automatic registration of the listener to publisher, based on the signature of the annotated method.
You could be tempted to show in your model the (automatic) association between the listener and the publisher. But IMHO, this would not reflect well your design, since your model would hardwire the magic, whereas your code is dynamic in this regard. This is why the simpler diagram I show above, seems more appropriate: the magic is caused by the common Event denominator. Just use the right intermetiary type.
If you want to show the dynamics of the interactions, it's a little more complex. You could for example use an sequence diagram, and encabulate the magic in a :SpringFramework
lifeline, and materialize the automatic registration with messages sent to this "virtual" lifeline. Moreover:
It's not the real details. But the readers of your diagram do not need to know these details (except if they'd belong to the spring development team). So keep it as simple as possible, but not more :-)
You must somehow have access to a time machine to produce all these answers :-)
@qwerty_so you made my day :-)