1

我尝试将 AOP 应用于 Struts2 动作类。我的配置是:

<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>

我的行动课是:

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;
}
}

我的方面是:

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;
}
}

当我在 tomcat6.x 服务器上执行此应用程序时,AOP 未应用于该操作类。

4

1 回答 1

2

我找到了解决方案。需要在classpath中添加struts2-spring-plugin-2.xxjar。该插件会自动将 struts.xml 中配置的所有动作类添加到spring container 中

于 2010-11-18T14:55:40.743 回答