I am trying to get a simple AOP example working, but I am confused with some of the logging output from Cglib2AOPProxy. The expected app output is
But, instead, I'm just getting:
The aspect is supposed to provide the "Hello, ".
I've attached the Eclipse project as a .zip below.
Here's what log4j is providing (abbreviated):
10:31:51,949 DEBUG [JdkRegexpMethodPointcut] Candidate is [test.TargetThread.utilityMethod]; pattern is [.*utilityMethod.*]; matched=true
10:31:51,949 DEBUG [Cglib2AopProxy] Unable to apply any optimisations to advised method public void test.TargetThread.utilityMethod() - using AOP_PROXY
Here's TargetThread:
Code:
package test;
public class TargetThread extends Thread {
@Override
public void run() {
utilityMethod();
}
public void utilityMethod() {
System.out.println("AOP");
}
}
And here's my applicationContext:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="thread.target" class="test.TargetThread" />
<bean id="advisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="advice.simple"/>
</property>
<property name="patterns">
<list>
<value>.*utilityMethod.*</value>
</list>
</property>
</bean>
<bean id="thread" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="thread.target" />
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
</bean>
<bean id="advice.simple" class="test.SimpleAdvice" />
</beans>
Here's my aspect:
Code:
package test;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class SimpleAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.print("Hello, ");
return invocation.proceed();
}
}
And finally, here's the TestCase I'm using to test this:
Code:
package test;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDriver extends TestCase {
private BeanFactory factory;
protected void setUp() throws Exception {
super.setUp();
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
// of course, an ApplicationContext is just a BeanFactory
factory = (BeanFactory) appContext;
}
public void testThread() throws InterruptedException {
TargetThread thread = (TargetThread) factory.getBean("thread");
thread.start();
Thread.sleep(500);
}
protected void tearDown() throws Exception {
super.tearDown();
}
}