Home > database >  calling a particular method from all setter methods
calling a particular method from all setter methods

Time:09-30

I have a scenario where i have to call a manual validation method from all the setter methods of a model class.

I have used lombok to enable getter and setter in my model class

eg.

@Data
class Model {
    int x;
    int y;
}

class Util {
    public static int validate(int x) {
        return x   1;
    }
}

I have this requirement that Util.validate(x) method should be called every time a setter method of my model class being called.

I don't want to write the setter and manually call the utility method

any optimal way for this available?

CodePudding user response:

You could use an aspect, but it's largely overkill.

https://www.javatpoint.com/spring-aop-tutorial

CodePudding user response:

to call that method in every case, one easy way we can go is using aspect, it will provide @Before @After @around @Pointcut

EX: @Before("forAllMethods() && exceptConfig() && exceptBeans()") public void data(){}

@Around("forAllMethods() && exceptConfig() && exceptBeans()") public void data(){}

  • Related