NameMatchMethodPointcutAdvisor example!

I admit, I am a slow learner!

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>spring-aop-examples</artifactId>
    <packaging>jar</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>bare-maven-project</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>
        <finalName>spring-aop-examples</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <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>

MyService.java
public interface MyService {
    void serve();
}

MyServiceImpl.java
public class MyServiceImpl implements MyService {
    @Override
    public void serve() {
        System.out.println("MyServiceImpl#serve");
    }
}

MyMethodBeforeAdviceImpl.java
import org.springframework.aop.MethodBeforeAdvice;
 
import java.lang.reflect.Method;
 
public class MyMethodBeforeAdviceImpl implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("MyMethodBeforeAdviceImpl#before");
    }
}

App.java
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
 
public class App {
    public static void main(String[] args) {
        final MyServiceImpl myService = new MyServiceImpl();
        final MethodBeforeAdvice myMethodBeforeAdvice = new MyMethodBeforeAdviceImpl();
 
        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvice(myMethodBeforeAdvice);
        proxyFactory.setTarget(myService);
 
        MyService proxyServiceFromFactory = (MyService) proxyFactory.getProxy();
        proxyServiceFromFactory.serve();
    }
}

In Action
MyMethodBeforeAdviceImpl#before
MyServiceImpl#serve

Wait! What Gives? There is no NameMatchMethodPointcutAdvisor in this example?

In the example above, we used the addAdvice method where the documentation states:
This will be wrapped in a DefaultPointcutAdvisor with a pointcut that always applies, and returned from the getAdvisors() method in this wrapped form. 
Note that the given advice will apply to all invocations on the proxy, even to the toString() method! Use appropriate advice implementations or specify appropriate pointcuts to apply to a narrower set of methods.

Lets use a NameMatchMethodPointcutAdvisor

App.java
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
 
public class App {
    public static void main(String[] args) {
        final MyServiceImpl myService = new MyServiceImpl();
        final MethodBeforeAdvice myMethodBeforeAdvice = new MyMethodBeforeAdviceImpl();
 
        final NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor = new NameMatchMethodPointcutAdvisor();
        nameMatchMethodPointcutAdvisor.setAdvice(myMethodBeforeAdvice);
        nameMatchMethodPointcutAdvisor.setMappedName("serve");
 
        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(nameMatchMethodPointcutAdvisor);
        proxyFactory.setTarget(myService);
 
        MyService proxyServiceFromFactory = (MyService) proxyFactory.getProxy();
        proxyServiceFromFactory.serve();
    }
}

Note that we have used the addAdvisor method, and not the addAdvice method!