Results 1 to 4 of 4

Thread: [newbie] matched=true, but no advice?

  1. #1
    Join Date
    May 2006
    Posts
    15

    Exclamation [newbie] 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 "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();
    	}
    
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2004
    Posts
    14

    Default

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

  3. #3
    Join Date
    May 2006
    Posts
    15

    Default

    Quote Originally Posted by serg-b
    So, you are using "internal" call. An interception works only for outside invocations. At this situation you can use "org.springframework.aop.framework.AopContext.curr entProxy()" to make an interceptable call. Also you should set "exposeProxy" 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

  4. #4
    Join Date
    Nov 2004
    Posts
    14

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •