Back Forum Reply New

matched=true, but no advice?

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
Hello, AOP

But, instead, I'm just getting:
AOP

The aspect is supposed to provide the quot;Hello, quot;.

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(quot;AOPquot;);
}
}
And here's my applicationContext:

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;!DOCTYPE beans PUBLIC quot;-//SPRING//DTD BEAN//ENquot; quot;dtd/spring-beans.dtdquot;gt;

lt;beansgt;
lt;bean id=quot;thread.targetquot; class=quot;test.TargetThreadquot; /gt;
lt;bean id=quot;advisorquot;    class=quot;org..aop.support.RegexpMethodPointcutAdvisorquot;gt;   lt;property name=quot;advicequot;gt;       lt;ref local=quot;advice.simplequot;/gt;   lt;/propertygt;   lt;property name=quot;patternsquot;gt;       lt;listgt;lt;valuegt;.*utilityMethod.*lt;/valuegt;       lt;/listgt;   lt;/propertygt;
lt;/beangt;
lt;bean id=quot;threadquot; class=quot;org..aop.framework.ProxyFactoryBeanquot;gt;
lt;property name=quot;targetquot; ref=quot;thread.targetquot; /gt;
lt;property name=quot;interceptorNamesquot;gt;
lt;listgt;
lt;valuegt;advisorlt;/valuegt;
lt;/listgt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;advice.simplequot; class=quot;test.SimpleAdvicequot; /gt;
lt;/beansgt;
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(quot;Hello, quot;);
return invocation.proceed();
}

}
And finally, here's the TestCase I'm using to test this:

Code:
package test;

import junit.framework.TestCase;

import org..beans.factory.BeanFactory;
import org..context.support.ClassPathXmlApplicationContext;

public class TestDriver extends TestCase {
private BeanFactory factory;

protected void setUp() throws Exception {
super.setUp();
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(       new String[] {quot;applicationContext.xmlquot;});
// of course, an ApplicationContext is just a BeanFactory
factory = (BeanFactory) appContext;
}

public void testThread() throws InterruptedException {
TargetThread thread = (TargetThread) factory.getBean(quot;threadquot;);
thread.start();
Thread.sleep(500);
}

protected void tearDown() throws Exception {
super.tearDown();
}

}So, you are using quot;internalquot; call. An interception works only for outside invocations. At this situation you can use quot;org..aop.framework.AopContext.curr  entProxy()quot; to make an interceptable call. Also you should set quot;exposeProxyquot; property of the ProxyFactoryBean.


Originally Posted by serg-bSo, you are using quot;internalquot; call. An interception works only for outside invocations. At this situation you can use quot;org..aop.framework.AopContext.curr  entProxy()quot; to make an interceptable call. Also you should set quot;exposeProxyquot; property of the ProxyFactoryBean.

OK, so since the utilityMethod() call is internal to the class, it cannot be intercepted, correct? Is there a way of configuring this to work without making the class aware that it is being proxied?

Respectfully,
Brice Ruth

You class mixes two responsibilities. You can split it into a thread executor and a bean impelementing some bisness logic. The last will be intercepted. This schema do not need making the class aware about proxing.
¥
Back Forum Reply New