JdkRegexpMethodPointcut example!

J-- D-- K-- Regexppp what?

Directory Layout
MacBook-Pro:springaopexamples koraytugay$ tree
.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── biz
        │       └── tugay
        │           └── springaopexamples
        │               ├── App.java
        │               ├── MyDummyAdviceImpl.java
        │               ├── bar
        │               │   ├── Bar.java
        │               │   └── BarImpl.java
        │               └── foo
        │                   ├── Foo.java
        │                   └── FooImpl.java
        └── resources
            └── beans.xml

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>${project.name}</artifactId>
    <packaging>jar</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>${project.name}</name>
    <url>http://www.tugay.biz</url>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
    </dependencies>
 
    <build>
        <directory>/Users/koraytugay/target/${project.name}</directory>
        <finalName>${project.name}</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.springaopexamples.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <project.name>springaopexamples</project.name>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

MyDummyAdviceImpl.java
package biz.tugay.springaopexamples;
 
import org.springframework.aop.AfterReturningAdvice;
 
import java.lang.reflect.Method;
 
public class MyDummyAdviceImpl implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("MyDummyAdviceImpl#afterReturning!");
        System.out.println("");
    }
}

FooImpl.java
package biz.tugay.springaopexamples.foo;
 
public class FooImpl implements Foo {
    @Override
    public void foo() {
        System.out.println("FooImpl#foo");
    }
 
    @Override
    public void bar() {
        System.out.println("FooImpl#bar");
    }
}

BarImpl.java
package biz.tugay.springaopexamples.bar;
 
public class BarImpl implements Bar {
    @Override
    public void foo() {
        System.out.println("BarImpl#foo");
    }
 
    @Override
    public void bar() {
        System.out.println("BarImpl#bar");
    }
}

beans.xml
<?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.xsd">
 
    <bean id="foo" class="biz.tugay.springaopexamples.foo.FooImpl"/>
    <bean id="bar" class="biz.tugay.springaopexamples.bar.BarImpl"/>
 
    <bean id="myRegExpPCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*foo.*"/>
    </bean>
 
    <bean id="myDummyAdvice" class="biz.tugay.springaopexamples.MyDummyAdviceImpl"/>
 
    <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut" ref="myRegExpPCut"/>
        <property name="advice" ref="myDummyAdvice"/>
    </bean>
 
    <bean id="fooProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="foo"/>
        <property name="interceptorNames">
            <list>
                <value>myAdvisor</value>
            </list>
        </property>
    </bean>
 
    <bean id="barProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="bar"/>
        <property name="interceptorNames">
            <list>
                <value>myAdvisor</value>
            </list>
        </property>
    </bean>
</beans>

App.java
package biz.tugay.springaopexamples;
 
import biz.tugay.springaopexamples.bar.Bar;
import biz.tugay.springaopexamples.foo.Foo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
 
    public static void main(String[] args) {
        final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
 
        final Foo foo = applicationContext.getBean("fooProxy", Foo.class);
        final Bar bar = applicationContext.getBean("barProxy", Bar.class);
 
        foo.foo();
        foo.bar();
        bar.foo();
        bar.bar();
    }
}

In Action
FooImpl#foo
MyDummyAdviceImpl#afterReturning!
 
FooImpl#bar
MyDummyAdviceImpl#afterReturning!
 
BarImpl#foo
MyDummyAdviceImpl#afterReturning!
 
BarImpl#bar

Notes

So why does FooImpl#foo matches? This is revealed when a break-point is introduced in JdkRegexpMethodPointcut#boolean matches(String pattern, int patternIndex)..


So the pattern is not matched only by method name, but rather fully qualified class name + method name!

How do I restrict the expression?
Try this:
<bean id="myRegExpPCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="pattern" value=".*foo.FooImpl.*"/>
    <property name="excludedPattern" value=".*bar.*" />
</bean>

And maybe show me how to use RegexpMethodPointcutAdvisor?
<bean id="myDummyAdvice" class="biz.tugay.springaopexamples.MyDummyAdviceImpl"/>
<bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="myDummyAdvice"/>
    <property name="pattern" value=".*foo.FooImpl.*"/>
</bean>

Hmm, ok. So what?
Well we have one less bean definition.. But we lost the excludePattern property!