Reputation: 73
I have bussiness class as follow:
class A {
public void sayHello(String name){
System.out.println("hello "+name);
}
public void openDoorForJack(){
System.out.println("door opened");
this.sayHello("Jack");
}
}
and aspect class as follow:
@Aspect
class Aspect {
@Pointcut("execution (* com..*.sayHello(String)) && args(name)")
public void beforeSayHelloPointCut(String name) {}
@Before("beforeSayHelloPointCut(name)")
public void beforeSayHello(String name) throws Throwable {
System.out.println(name+" is knocking");
}
}
after I have all those beans configured in spring,I turn LTW on using
<aop:aspectj-autoproxy/>
<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
of course aspectjrt,aspectj-weaver,spring-instrument are in position,-javaagent:path/spring-instrument.jar is passed to VM options and follow aop.xml is under META-INF
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in specific packages -->
<include within="*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.Aspect"/>
</aspects>
</aspectj>
When I run test like:
applicationContext.getBean(A.class).sayHello("Jack");
result seems perfect
Jack is knocking
Hello Jack
But when I run test that call sayHello inside itself
applicationContext.getBean(A.class).openDoorForJack();
no interception happens at all
door opened
Hello Jack
The reason way I use LTW is that I want methods call using "this" or "super" can be intercepted as well,but seems I failed to do so. Would someone help me point out the problem,is there something I missed?
----------------------------EDIT---------------------------------
After some debug I found where I got wrong
In real life,
I hava "class A" under package com.bussiness
and "class Aspect" under package com.aspect
I worte aop.xml like follow
<weaver>
<!-- only weave classes in specific packages -->
<include within="com.aspect.*"/>
</weaver>
which is not correct,it should be the package that contains classes need to be woven,after I changed it to
<weaver>
<!-- com package and its sub package all get woven-->
<include within="com..*"/>
</weaver>
It finally work
Upvotes: 2
Views: 649
Reputation: 3603
Try dynamic proxy, it should work. It's not easy but I think this is what you need.
More on this: How to use AOP to intercept a method call in super on an argument?
Upvotes: 0