Pure Spring AOP - MethodBeforeAdvice example!

together with ProxyFactory and no Application Context!

Directory Layout
.
├── pom.xml
└── src
    └── main
        └── java
            └── biz
                └── tugay
                    └── spashe
                        ├── App.java
                        ├── Foo.java
                        ├── FooImpl.java
                        └── MethorBeforeAdviceBarImpl.java

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>spashe</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>spashe</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.spashe.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>

Foo.java
package biz.tugay.spashe;
 
public interface Foo {
    void foo();
    void baz();
}

FooImpl.java
package biz.tugay.spashe;
 
public class FooImpl implements Foo {
    @Override
    public void foo() {
        System.out.println("Foo!");
    }
 
    @Override
    public void baz() {
        System.out.println("Baz!");
    }
}

MethorBeforeAdviceBarImpl.java
package biz.tugay.spashe;
 
import org.springframework.aop.MethodBeforeAdvice;
 
import java.lang.reflect.Method;
 
public class MethorBeforeAdviceBarImpl implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("Bar!");
    }
}

App.java
package biz.tugay.spashe;
 
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
 
public class App {
 
    public static void main(String[] args) {
        final MethodBeforeAdvice advice = new MethorBeforeAdviceBarImpl();
 
        final ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvice(advice);
 
        final Foo foo = new FooImpl();
        proxyFactory.setTarget(foo);
 
        final Foo fooProxy = (Foo) proxyFactory.getProxy();
        fooProxy.foo();
        fooProxy.baz();
    }
}

In Action
MacBook-Pro:spashe koraytugay$ mvn clean install exec:java -q
Bar!
Foo!
Bar!
Baz!