简体   繁体   English

春季建议不适用于某些类的某些方法

[英]Spring advice don't apply to some methods of the some classes

So, I have problem with adding the aspect to already created system. 因此,我在将方面添加到已创建的系统时遇到问题。 Problem - the pointcut doesn't work to some classes. 问题-切入点不适用于某些班级。 For example this code works good: 例如,这段代码运行良好:

<aop:config proxy-target-class="true">
        <aop:pointcut id="addSubmitListener"
                      **expression="execution (* com.solutions.foo.ClassA.methodA(..))"/>**
        <aop:aspect ref="hijackBeforeAddSubmitListenerBean">
            <aop:around method="proceedWhileNotDash" pointcut-ref="addSubmitListener"/>
        </aop:aspect>
    </aop:config>

ClassA defined as a bean in this applicationContext. 在此applicationContext中定义为bean的ClassA。

Now, other sample. 现在,其他示例。 This sample doesn't work. 该示例无效。

<aop:pointcut id="addSubmitListener"
                      expression="execution (* com.click.otherfoo.ClassB.methodB(..))"/>

Class B defined in other applicationContext, imported with <import resource="classpath*:... 在其他applicationContext中定义的类B,使用<import resource="classpath*:...

also one more difference methodB - has void type, and methodA - not 还有另一个区别methodB-具有void类型,而methodA-不

I just tried this which might or might not be your configuration. 我只是尝试过这个,可能是也可能不是您的配置。 But the code as mentioned by you works perfectly fine for me. 但是您提到的代码对我来说非常合适。 Here is what i have tried based on your question and mentioned information. 这是我根据您的问题和提及的信息尝试过的方法。

If i just change the bean definition in the anotherConfig.xml file, it works fine for ClassA as well. 如果我只是更改anotherConfig.xml文件中的bean定义,那么它对于ClassA也可以正常工作。 Works for ClassB too 也适用于ClassB

applicationContext.xml applicationContext.xml中

<import resource="classpath*:anotherConfig.xml"/>

<context:component-scan base-package="com.appContextexample"/>
<aop:aspectj-autoproxy/>

<aop:config proxy-target-class="true">
        <aop:pointcut id="addSubmitListener" expression="execution (* com.click.otherfoo.ClassB.methodB(..))"/>
        <aop:aspect ref="hijackBeforeAddSubmitListenerBean">
            <aop:around method="proceedWhileNotDash" pointcut-ref="addSubmitListener"/>
        </aop:aspect>
    </aop:config>

<bean id = "hijackBeforeAddSubmitListenerBean" class = "com.appContextexample.HijackBeforeAddSubmitListenerBean"/>

</beans>

anotherConfig.xml anotherConfig.xml

<bean id="classA" class="com.click.otherfoo.ClassB"></bean>

Application.java This is the starting point. Application.java这是起点。

public class Application {

   public static void main(final String[] args) {
      final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      final ClassB bean = (ClassB) context.getBean("classA");
      bean.methodB("test");

      System.out.println("appContext created" + bean);
   }

ClassB.java ClassB.java

package com.click.otherfoo;

public class ClassB {

   public void methodB(final String str) {
      System.out.println(str);
   }

}

And finally the Aspect. 最后是看点。

public class HijackBeforeAddSubmitListenerBean {

   public Object proceedWhileNotDash(final ProceedingJoinPoint pjp) throws Throwable {
      System.out.println("Called the aspect");
      pjp.proceed();
      return null;

   }

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

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