0

I'm creating a custom Metric Aspect so that I don't have to do METRIC.aboutTo("save", "order") in the code. My organization uses custom metric libraries.

So, I want to create a custom metric annotation and aspect to do this for me.

I'm looking to be able to add the annotation @PostgressMetric to both the class and the method. It would look something like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface PostgressMetric {

    String value() default "";
    String table() default "";

}

If the metric is on the class, then I want to use those values as "defaults". So you could have something like this:

@Repository
@PostgressMetric(table = "order")
public class OrderRepository {

   @PostgressMetric("save")
   public void save(Order order) {
   }
}

This is also a valid setup:

@Repository
public class OrderRepository {

   @PostgressMetric(table="order", value="save")
   public void save(Order order) {
   }
}

and of course this is also a valid setup:

@Repository
@PostgressMetric(table="order", value="save")
public class OrderRepository {

   public void save(Order order) {
   }
}

Now, in my aspect I want to get the annotation from the class, then get the annotation from the method. If the class has the annotation then get the table and value and use those as default values. If the method has the annotation then if the values aren't blank, use them to override the default values specified in the class annotation.

Is there a standard way to get the two annotations, or is it just a brute force get the class values and then get the method values and override them?

  • Do go through this [answer](https://stackoverflow.com/a/53452483/4214241) and explanation of that answer [here](https://stackoverflow.com/questions/61430240/how-does-this-execution-pointcut-expression-work) . With Spring AOP , kriegaex's answer is the better approach for your question – R.G May 08 '22 at 04:35
  • Welcome to SO. Please be advised to provide feedback and not just remain silent when someone comments or answers your question, trying to help you. It is a matter of basic courtesy. – kriegaex May 14 '22 at 09:03

0 Answers0