开发者

Spring aop with struts2

开发者 https://www.devze.com 2023-01-06 04:39 出处:网络
I am new to Spring aop and I decided to use aop to track my Struts2 Action cla开发者_如何学编程ss execution time. I have done the following things. But while running the application setter method of t

I am new to Spring aop and I decided to use aop to track my Struts2 Action cla开发者_如何学编程ss 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.


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消