Announcement Announcement Module
Collapse
No announcement yet.
Interesting behavior when AOP aspect bean implements an interface Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Interesting behavior when AOP aspect bean implements an interface

    I'm using spring 2.0.6 and got the following interesting stuff:

    When I:
    1) use AOP with jdk interface proxy (which means do not force it to use CGLIB)
    2) use xml schema-based AOP defining
    3) define the aspect back bean to implement an interface like Ordered
    4) define a relatively broad pointcut expression like * *(..)

    I've got the following error:

    ********* error message ********
    org.springframework.aop.AopInvocationException: Mismatch on arguments to advice method [public void test.Logger.log()]; pointcut expression [org.aspectj.weaver.internal.tools.PointcutExpressi onImpl@7c3885]; 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(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    ......
    ******** error message over ********

    I'm not sure if someone else also run into something like this. Can someone explain it? Is it a implementation limitation, designed to be like that or just the way I use it was not correct?

    I realize that there are workarounds for this such as forcing to use CGLIB or by defining a more restrictive pointcut. But isn't it a bit too tricky for a developer to use - I mean if some error like this happens in a medium size application, it could be very difficult to trace...

    ******* code **********
    package test;

    import org.springframework.core.Ordered;

    public class Logger implements Ordered
    {
    public void log()
    {
    System.out.println("Log It!");
    }

    public int getOrder()
    {
    return 1;
    }
    }

    ---

    package test;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class LoggerTarget
    {

    public void doIt()
    {
    System.out.println("do it!");
    }

    public static void main(String args[])
    {
    ApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "/context1.xml"});
    LoggerTarget logger = (LoggerTarget) context.getBean("loggerTarget");
    logger.doIt();
    }
    }

    ---

    <?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/schem...-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
    >

    <bean id="loggerTarget" class="test.LoggerTarget"/>
    <bean id="logger" class="test.Logger"/>

    <aop:config proxy-target-class="false">
    <aopointcut id="d2" expression="execution(* *(..))"/>
    <aop:aspect ref="logger">
    <aop:before pointcut-ref="d2" method="log"/>
    </aop:aspect>
    </aop:config>

    </beans>
    ******* code over **********

  • #2
    Anyone has a clue of this? It seems closely related to the specific AOP proxy implementation logic in Spring. It does not matter whether you restrict the class name like: * package.someclass. As long as you leave the method name pattern to "*", which is a very common use case, the error will happen. And if you dig into the issue to find out which method name pattern will work and which will not (besides "*"), you will find more interesting stuff like "d*(..)" will work while "t*(..)" will NOT. It makes me a little bit nervous...

    Comment

    Working...
    X