AspectJ with Spring with XML Configuration!

Autoproxy in action!

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>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.12</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>biz.tugay.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
package biz.tugay;
 
public interface MyService {
    void serve();
}

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

MyMethodBeforeAdviceImpl.java
package biz.tugay;
 
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
 
@Aspect
public class MyMethodBeforeAdviceImpl {
 
    @Before("execution(void biz.tugay.MyService.serve())")
    public void before() throws Throwable {
        System.out.println("biz.tugay.MyMethodBeforeAdviceImpl#before");
    }
}

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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
 
    <aop:aspectj-autoproxy/>
 
    <bean id="myMethodBeforeAdvice" class="biz.tugay.MyMethodBeforeAdviceImpl"/>
    <bean id="myService" class="biz.tugay.MyServiceImpl"/>
</beans>

In Action
biz.tugay.MyMethodBeforeAdviceImpl#before
biz.tugay.MyServiceImpl#serve