I setup LTW (load-time-weaving) with AspectJ and spring pretty quick and successfull. Here is the setup: beans.xml:
My service that will be autowired to a class:Code:<aop:aspectj-autoproxy /> <context:spring-configured /> <context:load-time-weaver /> <context:component-scan base-package="com.test.service" />
Parent class:Code:@Service public class MyService { }
The configurable class, that autowires the service and extends Bar.Code:public class Bar { }
And just a class that has got a referance to the parent class Bar:Code:@Configurable public class BarExtended extends Bar{ @Autowired private MyService service; public MyService getWeavedInObject(){ return service; } }
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:public class Foo { private Bar bar; public void setBar(Bar bar) { this.bar = bar; } }
This test runs green. BUT following test fails:Code:@Test public void simple(){ Foo foo = new Foo(); BarExtended barExtended = new 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.Code:@Test public void simple(){ Foo foo = new Foo(); BarExtended barExtended = new BarExtended(); foo.setBar(barExtended); assertNotNull("LTW didn't work.", barExtended.getWeavedInObject()); }
BTW, when I change the Foo class to use the BarExtended class (so no upcasting is needed):
The above test will work. Does anyone has an idea why AspjectJ behaves so strange, when a configurable object is upcasted?Code:public class Foo { private BarExtended bar; public void setBar(BarExtended bar) { this.bar = bar; } }
Follwing fails as well:
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() { Foo foo = new Foo(); BarExtended barExtended = new BarExtended(); Bar bar = (Bar) new BarExtended(); foo.setBar(bar); assertNotNull("LTW didn't work.", barExtended.getWeavedInObject()); }
Strange, isn't it?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()); }
Regards,
Andreas


Reply With Quote
