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

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

问题描述

我试图拦截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集成按照URL完成:

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)忽略构造函数[public org.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'的名称:否 找到匹配的豆子

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 ?

推荐答案

未调用通知方法,因为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)的代理.动作类.

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.

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

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