1

Struts2のアクションクラスにAOPを適用してみました。私の構成は次のとおりです。

<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

解決策を見つけました。クラスパスにstruts2-spring-plugin-2.xxjarを追加する必要があります。このプラグインは、struts.xml で構成されたすべてのアクション クラスをspring containerに自動的に追加します。

于 2010-11-18T14:55:40.743 に答える