Spring aop与struts2
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了这种问题,这个 帮助了我。
我强迫 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!
是的,这也适用于我,通过在 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