Warm tip: This article is reproduced from serverfault.com, please click

How to represent in UML that one class calls another class method when publishing spring event

发布于 2020-12-03 13:09:28

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?

enter image description here

Questioner
Valeriy K.
Viewed
0
Christophe 2020-12-04 05:23:05

The class 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:

You may therefore want to show a dependency to the common ApplicationEvent:

enter image description here

The Spring magic

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.

The interactions

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:

  • Event sending would then be a message from the publisher to the framework,
  • Event listening would be a message sent by the framework to one or several listener.

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 :-)