简体   繁体   English

带有struts2的Spring aop

[英]Spring aop with struts2

I am new to Spring aop and I decided to use aop to track my Struts2 Action class execution time.我是 Spring aop 的新手,我决定使用 aop 来跟踪我的 Struts2 Action 类执行时间。 I have done the following things.我做了以下几件事。 But while running the application setter method of the action class is not called .但是在运行时不会调用 action 类的应用程序setter 方法 Here is my code.这是我的代码。 xml configuration: xml配置:

<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"/>是的,这也适用于我,通过使用 XML 中的这一行将默认的 JDK 动态代理更改为 CGLIB:<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请参阅此链接中的“8.6 代理机制”部分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. 我意识到,这可能会造成新的问题,因此我迫使spring AOP使用CGLIB代理。 Now I can advice my struts2 actions! 现在,我可以建议我的struts2动作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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