Announcement Announcement Module
Collapse
No announcement yet.
JdkRegexpMethodPointcut Joinpoints Are Not Being Selected Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JdkRegexpMethodPointcut Joinpoints Are Not Being Selected

    Hello,

    I am trying to use an DefaultPointcutAdvisor with a
    JdkRegexpMethodPointcut (to match a joinpoint) and some Advice to
    advise an Audience bean about a target Performer bean.

    (Attached bellow is the sourcecode)

    The audienceAdvice specifies that Audience.takeSeats() and
    Audience.turnOffCellphones() should be called before a selected
    joinpoint.

    The audienceAdvice also specifies that Audience.applaud() should be
    called after a selected joinpoint.

    The joinpoints are selected using the "performancePointcut" bean,
    which is a JdkRegexpMethodPointcut which uses the regular expression
    ".*perform" to select the Performer.perform() method.

    Performer is an interface, and its implementation is the
    Instrumentalist class. The Instrument class's perform() method is
    called by Example2Test where it should activate the audienceAdvice.

    The expected output of running the Example2Test would be the following:

    --------------------------------------------------------------------------
    INFO Example2Test - Entering PerformanceTest
    INFO Audience - The audience is taking their seats.
    INFO Audience - The audience is turning off their Cellphones.
    INFO Instrumentalist - Playing Jingle Bells 2:
    INFO Saxophone - TOOT TOOT
    INFO Audience - CLAP CLAP CLAP CLAP CLAP
    INFO Example2Test - Exiting PerformanceTest
    --------------------------------------------------------------------------


    However when I run the code I get the following unexpected result in
    which the audienceAdvice is never applied:

    --------------------------------------------------------------------------
    INFO Example2Test - Entering PerformanceTest
    INFO Instrumentalist - Playing Jingle Bells 2:
    INFO Saxophone - TOOT TOOT
    INFO Example2Test - Exiting PerformanceTest
    --------------------------------------------------------------------------

    Is there something wrong with the regular expression (.*perform) I use
    to select the joinpoint?

    Thank you,
    Andrew J. Leer


    Code:
    package com.springinaction.springidol.classicspringaspects.example2;
    
    
    import org.apache.log4j.Logger;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    
    import com.springinaction.springidol.classicspringaspects.BaseSpringExamplesTest;
    import com.springinaction.springidol.classicspringaspects.example2.Performer;
    
    public class Example2Test extends BaseSpringExamplesTest {
    
    	public static Logger log = Logger.getLogger(Example2Test.class);
    
    	private Performer performer;
    
    	@Override
    	public void exampleSetup(ApplicationContext ctx) {
    		performer = (Performer) ctx.getBean("example2_performer");
    	}
    
    	@Test
    	public void performanceTest()
    			throws Exception
    	{
    		log.info("Entering PerformanceTest");
    		performer.perform();
    		log.info("Exiting PerformanceTest");
    	}
    }
    Example2Test.java

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	    xsi:schemaLocation="http://www.springframework.org/schema/beans
    	    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
    
    
       <!-- BEGIN: Example 2 -->
    
       	<!-- Begin: Advisor (Combines your advice and your pointcut) -->
    	<bean id="audienceAdvisor"
    		  class="org.springframework.aop.support.DefaultPointcutAdvisor">
    		<property name="advice" ref="audienceAdvice" />
    		<property name="pointcut" ref="performancePointcut"/>
    	</bean>
    	<!-- End: Advisor -->
    
        <!-- Begin: Advice -->
        <bean id="audienceAdvice"
              class="com.springinaction.springidol.classicspringaspects.example2.AudienceAdvice">
        	<property name="audience" ref="example2_audience" />
        </bean>
    	<!-- End: Advice -->
    
    	<!-- Begin: Pointcut to match Joinpoints -->
    	<bean id="performancePointcut"
    		  class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    		<property name="patterns">
    		   <list>
    		    	<value>.*perform</value>
    		    </list>
    		</property>
    	</bean>
    	<!-- End: Pointcut to match Joinpoints -->
    
    
       <bean id="example2_performer"
             class="com.springinaction.springidol.classicspringaspects.example2.Instrumentalist">
    		<property name="song" value="Jingle Bells 2" />
    		<property name="instrument" ref="example2_instrument" />
       </bean>
    
    	<bean id="example2_instrument"
    		  class="com.springinaction.springidol.classicspringaspects.example2.Saxophone">
    
    	</bean>
    
       	<bean id="example2_audience"
    	      class="com.springinaction.springidol.classicspringaspects.example2.Audience">
    
        </bean>
       <!-- END: Example 2 -->
    
       <!-- BEGIN: Example 3 -->
    
       <!-- END: Example 3 -->
    </beans>
    applicationContext.xml

    Code:
    package com.springinaction.springidol.classicspringaspects.example2;
    
    public interface Performer {
    		public void perform() throws PerformanceException;
    }
    Performer.java

    Code:
    package com.springinaction.springidol.classicspringaspects.example2;
    
    import org.apache.log4j.Logger;
    
    public class Instrumentalist implements Performer {
       public Instrumentalist() { }
    
       public static Logger log = Logger.getLogger(Instrumentalist.class);
    
       public void perform() throws PerformanceException {
    
            log.info("Playing " + song + ": ");
            instrument.play();
       }
    
    
    	private String song;
    	public void setSong(String song) {
    		this.song = song;
    	}
    
    
    	private Instrument instrument;
    	public void setInstrument(Instrument instrument) {
    	   this.instrument = instrument;
    	}
    
    }
    Instrumentalist.java

    Code:
    package com.springinaction.springidol.classicspringaspects.example2;
    
    import org.apache.log4j.Logger;
    
    public class Audience {
    
    	public static Logger log = Logger.getLogger(Audience.class);
    
    	public Audience() { }
    
    	// Executes before performance
    	public void takeSeats() {
    		log.info("The audience is taking their seats.");
    	}
    
    	// Executes before performance
    	public void turnOffCellPhones() {
    		log.info("The audience is turning off " +
    					   "their cellphones");
    	}
    
    	// Executes after performance
    	public void applaud() {
    		log.info("CLAP CLAP CLAP CLAP CLAP");
    	}
    
    	// Executes after bad performance
    	public void demandRefund() {
    		log.error("Boo! We want our money back!");
    	}
    }
    Audience.java

    Code:
    package com.springinaction.springidol.classicspringaspects.example2;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.MethodBeforeAdvice;
    import org.springframework.aop.AfterReturningAdvice;
    import org.springframework.aop.ThrowsAdvice;
    
    public class AudienceAdvice implements MethodBeforeAdvice,
    									   AfterReturningAdvice,
    									   ThrowsAdvice
    {
    	private Audience audience;
    
    
    	public void before(Method arg0, Object[] arg1, Object arg2)
    			throws Throwable {
    		// Executes after successful
    		audience.takeSeats();
    		audience.turnOffCellPhones();
    	}
    
    	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
    			Object arg3) throws Throwable {
    		audience.applaud();
    	}
    
    	public void afterThrowing(Throwable throwable) {
    		audience.demandRefund();
    	}
    
    	public void setAudience(Audience audience) {
    		this.audience = audience;
    	}
    
    }
    AudienceAdvice.java

  • #2
    Spring needs to proxy your target object(s) in order to apply the AOP advice. For example:

    Code:
    <bean id="example2_performer" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="target" ref="example2_performer_target"/>
      <property name="interceptorNames">
        <list>
          <value>audienceAdvisor</value>
        </list>
      </property>
    </bean>
    or:

    Code:
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

    Comment

    Working...
    X