All 4 Available Advice Types in Spring..

..! Yes I know I said 4! Check this if you do not believe me! It is not 5, it is 4. (long pause) unless(sss) you are using AspectJ support! Then you get AfterFinally, which is not available here!

Directory Layout
MacBook-Pro:springaopexamples koraytugay$ tree
.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── biz
        │       └── tugay
        │           └── springaopexamples
        │               ├── App.java
        │               ├── MyAfterReturningAdviceImpl.java
        │               ├── MyMethodBeforeAdviceImpl.java
        │               ├── MyMethodInterceptorImpl.java
        │               ├── MyThrowsAdviceImpl.java
        │               └── manager
        │                   ├── FooManagerImpl.java
        │                   └── Manager.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>
 
    <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>

Manager.java
package biz.tugay.springaopexamples.manager;
 
public interface Manager {
    void foo();
    void fooThrowsException() throws Exception;
}

FooManagerImpl.java
package biz.tugay.springaopexamples.manager;
 
public class FooManagerImpl implements Manager {
    @Override
    public void foo() {
        System.out.println("FooManagerImpl#foo");
    }
 
    @Override
    public void fooThrowsException() throws Exception {
        throw new Exception("Some exception");
    }
}

MyMethodBeforeAdviceImpl.java
package biz.tugay.springaopexamples;
 
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 applied ==");
    }
}

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

MyThrowsAdviceImpl.java
package biz.tugay.springaopexamples;
 
import org.springframework.aop.ThrowsAdvice;
 
public class MyThrowsAdviceImpl implements ThrowsAdvice {
 
    public void afterThrowing(Exception ex) {
        System.out.println("== MyThrowsAdviceImpl applied ==");
    }
}

MyMethodInterceptorImpl.java
package biz.tugay.springaopexamples;
 
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
 
public class MyMethodInterceptorImpl implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("== Method Interceptor, before proceed ==");
        final Object proceed = invocation.proceed();
        System.out.println("== Method Interceptor, after proceed ==");
        return proceed;
    }
}

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="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
 
    <!-- Advices -->
    <bean id="myMethodBeforeAdvice" class="biz.tugay.springaopexamples.MyMethodBeforeAdviceImpl"/>
    <bean id="myAfterReturningAdvice" class="biz.tugay.springaopexamples.MyAfterReturningAdviceImpl"/>
    <bean id="myThrowsAdvice" class="biz.tugay.springaopexamples.MyThrowsAdviceImpl"/>
    <bean id="myMethodInterceptorImpl" class="biz.tugay.springaopexamples.MyMethodInterceptorImpl"/>
 
    <!-- Advisors -->
    <bean id="parentAdvisorConfiguration" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
          abstract="true">
        <property name="pattern" value="(biz.tugay.springaopexamples.manager).*foo.*"/>
    </bean>
 
    <bean id="myMethodBeforeAdvisor" parent="parentAdvisorConfiguration">
        <property name="advice" ref="myMethodBeforeAdvice"/>
    </bean>
 
    <bean id="myAfterReturningAdvisor" parent="parentAdvisorConfiguration">
        <property name="advice" ref="myAfterReturningAdvice"/>
    </bean>
 
    <bean id="myAfterThrowingAdvisor" parent="parentAdvisorConfiguration">
        <property name="advice" ref="myThrowsAdvice"/>
    </bean>
 
    <bean id="myMethodInterceptorAdvisor" parent="parentAdvisorConfiguration">
        <property name="advice" ref="myMethodInterceptorImpl"/>
    </bean>
 
    <bean id="fooManager" class="biz.tugay.springaopexamples.manager.FooManagerImpl"/>
</beans>

App.java
package biz.tugay.springaopexamples;
 
import biz.tugay.springaopexamples.manager.Manager;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
 
    public static void main(String[] args) {
        final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        final Manager fooManager = applicationContext.getBean("fooManager", Manager.class);
        fooManager.foo();
        try {
            fooManager.fooThrowsException();
        } catch (Exception e) {
            System.out.println("Exception caught!");
        }
    }
}

In Execution
== MyMethodBeforeAdviceImpl applied ==
== Method Interceptor, before proceed ==
FooManagerImpl#foo
== Method Interceptor, after proceed ==
== MyAfterReturningAdviceImpl applied ==
== MyMethodBeforeAdviceImpl applied ==
== Method Interceptor, before proceed ==
== MyThrowsAdviceImpl applied ==
Exception caught!