Struts 2 action方法拦截使用Spring AOP [英] Struts 2 action method intercepting using Spring AOP

查看:34
本文介绍了Struts 2 action方法拦截使用Spring AOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拦截 Struts2 Action 类的方法,以使用 Spring AOP 打印方法开始和方法结束指示语句.

I'm trying to intercept the Struts2 Action class's methods to print the method start and method end indication statement using Spring AOP.

事实是,我的 Struts2 动作实例也是 Spring bean(Struts2 和 Spring 集成按照以下网址完成:http://www.mkyong.com/struts2/struts-2-spring-integration-example/).AOP配置如下:

The fact is , my Struts2 actions instance are also Spring beans (Struts2 and Spring integration done as per the url: http://www.mkyong.com/struts2/struts-2-spring-integration-example/). AOP configurations is as below:

<bean id="testAdviceBean" class="com.tcs.oss.plugins.SimpleAdvice"> 
</bean>

<aop:config>
  <aop:aspect ref="testAdviceBean" order="200">

     <aop:pointcut id="testPoint2"
           expression="execution(java.lang.String com.test..DeviceAction.*(..))"
     /> 

     <aop:around pointcut-ref="testPoint2" method="loggingAdvice" />  

  </aop:aspect>
</aop:config>

在建议方法 loggingAdvice 中,我尝试使用 ProceedingJoinPoint API 打印方法 START 和方法 END 语句.建议方法根本没有被调用......相反,它以通过struts默认拦截器链后出现如下错误...

In the advice method loggingAdvice , I'm trying to print the method START and method END statement using the ProceedingJoinPoint API.The advice method is not called at all ... instead it's ending up with the error below after going through the struts default interceptor chain ...

但我收到以下错误跟踪:

But I'm getting the below ERROR TRACE:

09:26:49,093 跟踪[org.springframework.beans.factory.support.DefaultListableBeanFactory](http-01h3463916-172.20.211.235-8543-5) 忽略构造函数 [publicorg.apache.struts2.dispatcher.ServletDispatcherResult(java.lang.String)]bean 'org.apache.struts2.dispatcher.ServletDispatcherResult':org.springframework.beans.factory.UnsatisfiedDependencyException:创建带有名称的 bean 时出错'org.apache.struts2.dispatcher.ServletDispatcherResult':不满意依赖项通过构造函数参数表示,类型为索引 0[java.lang.String]:: 没有匹配的 [java.lang.String] 类型的 bean发现依赖项:预计至少有 1 个符合条件的 bean此依赖项的自动装配候选. 依赖项注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:否为依赖找到了类型为 [java.lang.String] 的匹配 bean:预计至少有 1 个 bean 有资格作为自动装配候选这种依赖.依赖注释:{}

09:26:49,093 TRACE [org.springframework.beans.factory.support.DefaultListableBeanFactory] (http-01h3463916-172.20.211.235-8543-5) Ignoring constructor [public org.apache.struts2.dispatcher.ServletDispatcherResult(java.lang.String)] of bean 'org.apache.struts2.dispatcher.ServletDispatcherResult': org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.apache.struts2.dispatcher.ServletDispatcherResult': Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

09:26:49,095 跟踪[org.springframework.beans.factory.support.DefaultListableBeanFactory](http-01h3463916-172.20.211.235-8543-5) 不是自动装配属性bean的'urlHelper''org.apache.struts2.dispatcher.ServletDispatcherResult' 按名称:无找到匹配的 bean

09:26:49,095 TRACE [org.springframework.beans.factory.support.DefaultListableBeanFactory] (http-01h3463916-172.20.211.235-8543-5) Not autowiring property 'urlHelper' of bean 'org.apache.struts2.dispatcher.ServletDispatcherResult' by name: no matching bean found

09:26:49,100 调试[org.apache.struts2.dispatcher.ServletDispatcherResult](http-01h3463916-172.20.211.235-8543-5) 转发到位置/General/error.jsp

09:26:49,100 DEBUG [org.apache.struts2.dispatcher.ServletDispatcherResult] (http-01h3463916-172.20.211.235-8543-5) Forwarding to location /General/error.jsp

如果我只是删除上面的 AOP 配置,它就可以正常工作.我做错了什么?

If I just remove the above AOP configurations, It's just working fine. What I'm doing wrong ?

推荐答案

Advice 方法没有被调用,因为 Action 类是从 ActionSupport 类扩展而来的,而 ActionSupport 类又具有接口实现......所以,在这种情况下,一个 JDK 代理是为 Action 类创建的 -- 这种类型的代理没有任何特定于 Action 类的方法(非继承).

The advice method is not called because Action class was extending from ActionSupport class which in turn has a interface implementations... So , in this case, A JDK proxy was created for the Action class -- This type of proxy doesn't have any method specific(non-inheritance) to the Action class.

在AOP配置中添加proxy-target-class="true"属性,使Spring生成基于CGLib(需要在Classpath中添加CGLib)的代理..现在也有非继承方法了,Action 类.

Adding proxy-target-class="true" attribute, in the AOP configuration, made the Spring to generate CGLib(need to add CGLib in the Classpath) based proxy.. which now has the non-inheritance methods too, of the Action class.

这篇关于Struts 2 action方法拦截使用Spring AOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆