简体   繁体   中英

Spring aop with struts2

I am new to Spring aop and I decided to use aop to track my Struts2 Action class execution time. I have done the following things. But while running the application setter method of the action class is not called . Here is my code. xml configuration:

<aop:aspectj-autoproxy/>
<bean id="myAspect" class="abc.xyz.ActionClassAspect"/>
<aop:config>
    <aop:pointcut id="actionClassPointcut" expression="execution(public * abc.xyz.action.*.*(..))
        and !execution(public * abc.xyz.action.*Action.get*(..))
        and !execution(public * abc.xyz.action.*Action.set*(..))"/>
    <aop:around pointcut-ref="actionClassPointcut" method="doActionClassProfilling"/>
</aop:config>

Aspect:

public Object doActionClassProfilling(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    long end = System.currentTimeMillis();
    System.out.println(proceedingJoinPoint.getClass()+" TIME: "+(end-start));
    return returnValue;
}

Action Class:

 private String userID, password;
 @Override
 public String execute() throws Exception {
     try {
         LoginService loginService = LoginService.getInstance();;
         UserProfile userProfile = loginService.validateUser(userID, password);
         Map<String, Object> sessionMap =  ActionContext.getContext().getSession();
         sessionMap.put("USER_PROFILE", userProfile);
         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;
 }

Thanks in advance.

Yes, this works for me as well, by changing the default JDK dynamic proxies into CGLIB with this line in XML: <aop:aspectj-autoproxy proxy-target-class="true"/>

See section "8.6 Proxying mechanisms" from this link https://docs.spring.io/spring-framework/docs/4.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable

I had this kind of issue and This helped me. I forced spring AOP to used CGLIB proxy, being aware of the new problems this could create. Now I can advice my struts2 actions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM