Announcement Announcement Module
Collapse
No announcement yet.
aop:before tag doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • aop:before tag doesn't work

    i am new to spring aop. when i read spring in action,i try to do what it tells me to do. i thought i was doing exactly what it tells me to do, but mistakes come.

    applicationContext.xml
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    <bean id="worker" class="test.Worker"/>
    <aop:config>
    <aop:aspect ref="worker">
    <aopointcut id="testPointcut" expression="execution(* *.test())"/>
    <aop:before method="startWork" pointcut-ref="testPointcut" />
    <aop:after-returning method="stopWork" pointcut-ref="testPointcut"/> </aop:aspect>
    </aop:config>
    </beans>

    test class

    public class Test
    {
    public static void main(String[] args)
    {

    ApplicationContext acx=new ClassPathXmlApplicationContext("applicationContext .xml");
    Work worker=(Work)acx.getBean("worker");
    worker.test();
    }

    }

    exception
    Exception in thread "main" org.springframework.aop.AopInvocationException: Mismatch on arguments to advice method [public void test.Worker.startWork()]; pointcut expression [org.aspectj.weaver.internal.tools.PointcutExpressi onImpl@153c375]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
    Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.aspectj.AbstractAspectJAdv ice.invokeAdviceMethodWithGivenArgs(AbstractAspect JAdvice.java:576)
    at org.springframework.aop.aspectj.AbstractAspectJAdv ice.invokeAdviceMethod(AbstractAspectJAdvice.java: 555)
    at org.springframework.aop.aspectj.AspectJMethodBefor eAdvice.before(AspectJMethodBeforeAdvice.java:39)
    at org.springframework.aop.framework.adapter.MethodBe foreAdviceInterceptor.invoke(MethodBeforeAdviceInt erceptor.java:49)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :161)
    at org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:89)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :161)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy0.test(Unknown Source)
    at test.Test.main(Test.java:19)

  • #2
    Hi,

    Your example works perfectly fine for me (I tested it with Spring 2.0.8, AspectjWeaver 1.5.3, and CGLib 2.1_3).

    Can you provide more information on your environment?

    Comment


    • #3
      Originally posted by Stefan Schmidt View Post
      Hi,

      Your example works perfectly fine for me (I tested it with Spring 2.0.8, AspectjWeaver 1.5.3, and CGLib 2.1_3).

      Can you provide more information on your environment?
      Hi
      Thanks for you reply.
      Yesterday,I found out that the example's exception was thrown because of the pointcut expression.
      excution shoud be execution(* *.Worker.test()) instead of
      execution(* *.test()).If I replace the class name(here Worker) with '*' it won't work.I can replace the package and method with '*' but I cannot do that to class name.
      This works well: execution(* *.Worker.*()) .
      This doesn't: execution(* test.*.test()) . the first test is package.
      As you said, this example works fine for you. I wonder it is the problem of
      version of environment or jars. Expecting more info from you.
      Here is my environment information:AspectjWeaver 1.5.3, spring version 2.0.5,
      CGLib 2.1_3
      p.s. English is not my native language,maybe it is hard to understand my expression.

      Comment


      • #4
        I have tried the same test with Spring 2.0.5 and it works fine. Do you have anything else set up?

        The following expressions work (for Spring managed beans of course):
        Code:
        execution(* test.*.test())
        calls all test() methods in all classes within the test package

        Code:
        execution(* *.test())
        calls all test() methods in all classes in all packages

        Code:
        execution(* test())
        calls all test() methods in all classes in all packages

        Code:
        execution(* *..*Worker.test())
        calls all test() methods in all classes ending with Worker in all packages

        There are many more possibilities. Have a look here Spring AOP reference examples section

        Comment


        • #5
          The previous example may not be so clear.Here I give another example,but mistakes still exist.The following are the codes.
          Code:
          package myaop;
          
          public interface IAudience
          {
          	public void takeSeat();
          	public void showTicket();
          	public void applaud();
          }
          Code:
          package myaop;
          
          public class Audience implements IAudience
          {
          	public void applaud()
          	{
          		System.out.println("give applause after show!");
          	}
          	public void showTicket()
          	{
          		System.out.println("show your ticket before show!");
          	}
          	public void takeSeat()
          	{
          		System.out.println("then take your seat before show!");
          	}
          }
          Code:
          package myaop;
          
          public interface IPerformer
          {
          	public void giveShow();
          }
          Code:
          package myaop;
          
          public class Performer implements IPerformer
          {
          	public void giveShow()
          	{
          		System.out.println("I am going to show now!");
          	}
          }
          Code:
          package testmyaop;
          
          import myaop.IPerformer;
          
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          
          public class TestMyaop
          {
          	public static void main(String[] args)
          	{
          		ApplicationContext acx=new ClassPathXmlApplicationContext("applicationContext.xml");
          		IPerformer performer=(IPerformer) acx.getBean("performer");
          		performer.giveShow();
          	}
          }
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
          	xmlns="http://www.springframework.org/schema/beans"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xmlns:aop="http://www.springframework.org/schema/aop"
          	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
          <bean id="performer" class="myaop.Performer"/>
          <bean id="audience" class="myaop.Audience"/>
          
          <aop:config>
          <aop:aspect ref="audience">
          <aop:pointcut id="testPointcut" expression="execution(* myaop.Performer.*(. .))"/>
          <aop:before method="showTicket" pointcut-ref="testPointcut" />
          <aop:before method="takeSeat" pointcut-ref="testPointcut" />
          <aop:after-returning method="applaud" pointcut-ref="testPointcut" />
          </aop:aspect>
          </aop:config> 
          </beans>
          applicationcontext.xml,if configed as before it will work.but when
          expression="execution(* myaop.*.*(. .))"/>
          the same exception will be thrown.Is it strange? all the codes are here.
          Will you find the reason for me ?thanks!

          Comment


          • #6
            Hi,

            Yes this seems to be an issue with the spring-2.0.*.jar versions. This can be overcome by switching to the latest spring version (spring-2.5.2.jar). Your example will work there.

            Comment


            • #7
              Hi Stefan Schmidt
              Thanks for your kindness and help.But I still have a question.
              the PDF of "spring in action" was published before the release of spring 2.5,
              its contents are all based on spring2.0. But it seems that all examples works
              fine in PDF documentation.

              Comment


              • #8
                Actually, these books are usually written against early versions or release candidates. So in this case if you try spring-2.0-rc2.jar it works.

                But it is still _highly_ recommended to use the latest version (i.e. 2.5.2) since this contains all bug fixes including the one you encountered.

                Also, I forgot to mention that in your second example the target object to be proxied implements an interface which means that JDK dynamic proxy will be used by default. Hence your CGLIB dependency is gone and you can remove it from the classpath.

                Comment


                • #9
                  That is very kind of you for replying !

                  Comment

                  Working...
                  X