Ayush Bilala
Ayush Bilala

Reputation: 151

Regarding Struts2 Spring AOP Logger

In Strtus2 Action class I am having methods with name like getData(), getDetails(). Along with these I am having several getter/setters in my action class. I want to use Spring's AOP functionality for logging purpose. I need to maintain Log for getData() and getDetails() method which in turn are calling the service class but I don't want to maintain Log for the getter/setters which are present in the action class.
I don't want to hard code method name in my Aspect. Don't have any clue of how to implement this. Please help me out.

My AOP Logger class is like :

@Before("myPointCut() && allService()")
public void logEntryService(JoinPoint joinPoint) {
    System.out.println("Start of " + joinPoint.getSignature().getName() + "() of "+joinPoint.getThis());
    start = System.currentTimeMillis();
}


@After("myPointCut() && allService()")
public void logExitService(JoinPoint joinPoint) {
    System.out.println("End of " + joinPoint.getSignature().getName() + "() of "+joinPoint.getThis());
    start = System.currentTimeMillis();
}

@Pointcut("@annotation(com.ing.trialbal.log.LogThis)")
public void myPointCut() {
    System.out.println("*************My PointCut");
}

@Pointcut("execution(* com.ing.trialbal.*.*.*(..))")
public void allService() {
    System.out.println("************All service");
}

LogThis.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogThis {

}

But this has created very tight coupling in my application as I always have to write @Logthis on every method in service and dao for having their logs.

Upvotes: 1

Views: 345

Answers (1)

lschin
lschin

Reputation: 6811

Pointcut bound with @annotation(). Something like

Annotation class

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

AOP Logger

@Pointcut("@annotation(com.Loggable)") //  && execution( ... )

Action class

@Loggable
Object getData() {}

@Loggable
Object getDetails() {}

Upvotes: 1

Related Questions