Maheshkumar
Maheshkumar

Reputation: 61

Spring AOP with Struts2 Action

I tried to apply AOP to Struts2 action classes. My configurations are:

<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="actionClassAspect" class="com.rpm.application.profiling.ActionClassAspect"/>
<aop:config>
<aop:pointcut id="actionClassPointcut" expression="execution(public * com.rpm..action.*.*(..))
    and !execution(public * com.rpm..action.*.get*(..))
    and !execution(public * com.rpm..action.*.set*(..))
    and !within(com.rpm..profiling.*)"/>

<aop:aspect id="actionAspect" ref="actionClassAspect">
    <aop:around method="doAspect" pointcut-ref="actionClassPointcut"/>
</aop:aspect>

my action class is:

package com.rpm.application.common.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class ApplicationLoginAction extends ActionSupport {
private String userID, password;

@Override
public String execute() throws Exception {
    try {
        //validation logic
        System.out.println("Login success");
        return SUCCESS;
    } catch(Exception e) {
        return ERROR;
    }
}

public String getUserID() {
    return userID;
}

public void setUserID(String userID) {
    this.userID = userID;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
}

my aspect is:

package com.rpm.application.profiling;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public abstract class ActionClassAspect {
public Object doAspect(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    long end = System.currentTimeMillis();
    System.out.println(" " + proceedingJoinPoint.getTarget().getClass() + " KIND:" + proceedingJoinPoint.getSignature().toShortString() + " TIME: " + (end - start));
    return returnValue;
}
}

When I executing this application on tomcat6.x server AOP is not applied to that action class.

Upvotes: 1

Views: 2488

Answers (1)

Maheshkumar
Maheshkumar

Reputation: 61

I found the solution. Need to add struts2-spring-plugin-2.x.x.jar in classpath. This plug-in will add automatically all action classes configured in struts.xml into spring container.

Upvotes: 2

Related Questions