Spring aop与struts2

发布于 2024-09-08 15:56:45 字数 1817 浏览 4 评论 0原文

我是 Spring aop 的新手,我决定使用 aop 来跟踪我的 Struts2 Action 类的执行时间。我做了以下几件事。但是在运行应用程序时不会调用操作类的setter方法。 这是我的代码。 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>

方面:

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

操作类:

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

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2024-09-15 15:56:45

我遇到了这种问题,这个 帮助了我。
我强迫 Spring AOP 使用 CGLIB 代理,意识到这可能会产生新问题。现在我可以建议我的 struts2 操作了!

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!

孤千羽 2024-09-15 15:56:45

是的,这也适用于我,通过在 XML 中使用以下行将默认的 JDK 动态代理更改为 CGLIB:

请参阅此链接中的“8.6 代理机制”部分

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文