Announcement Announcement Module
Collapse
No announcement yet.
AspectJ and Spring LTW doesn't work when upcasting Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AspectJ and Spring LTW doesn't work when upcasting

    I setup LTW (load-time-weaving) with AspectJ and spring pretty quick and successfull. Here is the setup: beans.xml:

    Code:
    <aop:aspectj-autoproxy />
    <context:spring-configured />
    <context:load-time-weaver />
    <context:component-scan base-package="com.test.service" />
    My service that will be autowired to a class:

    Code:
    @Service
    public class MyService {
    }
    Parent class:

    Code:
    public class Bar {
    }
    The configurable class, that autowires the service and extends Bar.

    Code:
    @Configurable
    public class BarExtended extends Bar{
        @Autowired
        private MyService service;
        public MyService getWeavedInObject(){
            return service;
        }
    }
    And just a class that has got a referance to the parent class Bar:

    Code:
    public class Foo {
        private Bar bar;
        public void setBar(Bar bar) {
            this.bar = bar;
        }
    }
    And a successfull test case. It just creates a instance of BarExtended and checks if the LTW worked. The Foo class does nothing. The working JVM parameter is: -javaagent:"${env_var:USERPROFILE}\.m2\repository\o rg\springframework\spring-instrument\3.1.0.RELEASE\spring-instrument-3.1.0.RELEASE.jar"

    Code:
    @Test
    public void simple(){
        Foo foo = new Foo();
        BarExtended barExtended = new BarExtended();
        assertNotNull("LTW didn't work.", barExtended.getWeavedInObject());
    }
    This test runs green. BUT following test fails:

    Code:
    @Test
    public void simple(){
        Foo foo = new Foo();
        BarExtended barExtended = new BarExtended();
        foo.setBar(barExtended);
        assertNotNull("LTW didn't work.", barExtended.getWeavedInObject());
    }
    I just insert the line where the class BarExtended is set to Foo. The upcast make the AspjectJ not working.

    BTW, when I change the Foo class to use the BarExtended class (so no upcasting is needed):

    Code:
    public class Foo {
        private BarExtended bar;
        public void setBar(BarExtended bar) {
            this.bar = bar;
        }
    }
    The above test will work. Does anyone has an idea why AspjectJ behaves so strange, when a configurable object is upcasted?

    Follwing fails as well:

    Code:
    @Test
    public void simple() {
        Foo foo = new Foo();
        BarExtended barExtended = new BarExtended();
        Bar bar = (Bar) new BarExtended();
        foo.setBar(bar);
        assertNotNull("LTW didn't work.", barExtended.getWeavedInObject());
    }
    A different BarExtended object is set to Foo and the first barExtended object is ignored by AspectJ. BUT using reflection to instantiate BarExtended works:

    Code:
    @Test
    public void simple() throws InstantiationException, IllegalAccessException{
        Foo foo = new Foo();
        Bar barExtended = (Bar) BarExtended.class.newInstance();
        foo.setBar(barExtended);
        assertNotNull("LTW didn't work.", ((BarExtended)barExtended).getWeavedInObject());
    }
    Strange, isn't it?

    Regards,

    Andreas
Working...
X