0

The task is to add an additional functionality to some methods from a bunch of spring-boot projects (all are under my control), avoiding naive copy-pasting of that functionality in every and each project, and - as mentioned in the topic - in an idiomatic way.

More concretely, I need to perform some logic before/after methods of interest.

So, in the example below I would like to do something extra before/after every method.

public class ClassName {

    public Object methodName() {
        ...
    }

    public Object anotherMethodName() {
        ...
    }
}

If there were only one project, a simple @Around aspect would be more than sufficient.

But for the given circumstances (multiple projects) I was thinking about something along the lines:

  1. create a custom spring-boot starter
  2. the starter will include new annotations to mark needed methods/classes, so the example from above could look like
public class ClassName {

    @MethodLevelAnnotation
    public Object methodName() {
        ...
    }

    @MethodLevelAnnotation
    public Object anotherMethodName() {
        ...
    }
}

or

@ClassLevelAnnotation
public class ClassName {

    public Object methodName() {
        ...
    }

    public Object anotherMethodName() {
        ...
    }
}
  1. the starter will programmatically register an advice for the aforementioned annotations (e.g. as suggested in this answer

With that in place the starter just need to be added to other projects dependencies and classes/methods must be tagged with the new Annotations.

Would it be a viable solution or I'm barking up a wrong tree here?

Stanislau
  • 317
  • 1
  • 4
  • 14

0 Answers0