VigneshBala
VigneshBala

Reputation: 35

Using AOP to invoke common operation before selective methods

I am doing design for a solution currently, but having a doubt in the approach for some requirement(in a Spring java application).

My requirement is to do a common process (like parsing and persisting in to db) before call of a method. The catch is that the application should have a capability to configure whether or not to call this method before a particular operation. (This will be configurable centrally as we are expecting frequent changes).

I am thinking of using AOP advice to call this method for selective methods.

Please let me know if there are any better approach to this requirement. Also please let me know whether its possible to maintain centralized configuration in xml in AOP.

Thanks in advance.

Upvotes: 0

Views: 134

Answers (1)

Vinay
Vinay

Reputation: 2817

Yes. Use AOP.

Your method in aspect class might look like this

@Before("within(pkg1..*)")
public Object  something(ProceedingJoinPoint joinPoint) throws Throwable { 
{
    if(false){
        throw SomeException; // returns with exception; make sure to catch this
    } else {
        joinPoint.proceed(); // Proceeds to execute the method
    }

    joinPoint.proceed();
}

Upvotes: 1

Related Questions