BeanNameAutoProxyCreator & DefaultAdvisorAutoProxyCreator examples!

Keyword here is: AutoProxyCreator!

Start without any AutoProxy!

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>

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

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

FooManagerImpl.java
package biz.tugay.springaopexamples.manager;
 
public class FooManagerImpl implements Manager {
    @Override
    public void foo() {
        System.out.println("FooManagerImpl#foo");
    }
 
    @Override
    public void man() {
        System.out.println("FooManagerImpl#man");
    }
}

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">
 
    <!-- Advice -->
    <bean id="myAfterAdvice" class="biz.tugay.springaopexamples.MyAfterAdviceImpl"/>
 
    <!-- Advisor -->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="myAfterAdvice"/>
        <property name="pattern" value="(biz.tugay.springaopexamples.manager).*foo.*"/>
    </bean>
 
    <bean id="fooManager" class="biz.tugay.springaopexamples.manager.FooManagerImpl"/>
 
    <bean id="fooManagerProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value="fooManager"/>
        <property name="interceptorNames">
            <list>
                <value>myAdvisor</value>
            </list>
        </property>
    </bean>
</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("fooManagerProxy", Manager.class);
        fooManager.foo();
        fooManager.man();
    }
}

Execution
FooManagerImpl#foo
== After Advice Applied ==
FooManagerImpl#man

Notes
This is pretty straight forward.. We are using the ProxyFactoryBean.

Introducing the BeanNameAutoProxyCreator 

We need to change the beans.xml slightly..
<?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">
 
    <!-- Advice -->
    <bean id="myAfterAdvice" class="biz.tugay.springaopexamples.MyAfterAdviceImpl"/>
 
    <!-- Advisor -->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="myAfterAdvice"/>
        <property name="pattern" value="(biz.tugay.springaopexamples.manager).*foo.*"/>
    </bean>
 
    <bean id="fooManager" class="biz.tugay.springaopexamples.manager.FooManagerImpl"/>
 
    <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames" value="fooManager"/>
        <property name="interceptorNames">
            <list>
                <value>myAdvisor</value>
            </list>
        </property>
    </bean>
</beans>

and App.java very slightly..
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();
        fooManager.man();
    }
}

In Action
FooManagerImpl#foo
== After Advice Applied ==
FooManagerImpl#man

Notes
This is some improvement over the ProxyFactoryBean, but not a huge one.. But perhaps the advantage comes with the fact that you can also define beanNames as follows..
<property name="beanNames" value="*Manager"/>

Introducing the DefaultAdvisorAutoProxyCreator

We will again change the 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"/>
 
    <!-- Advice -->
    <bean id="myAfterAdvice" class="biz.tugay.springaopexamples.MyAfterAdviceImpl"/>
 
    <!-- Advisor -->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="myAfterAdvice"/>
        <property name="pattern" value="(biz.tugay.springaopexamples.manager).*foo.*"/>
    </bean>
 
    <bean id="fooManager" class="biz.tugay.springaopexamples.manager.FooManagerImpl"/>
</beans>

Notes
This is a huge improvement, no? Lets try adding a new class to the manager package..

AnotherMngr.java
package biz.tugay.springaopexamples.manager;
 
public interface AnotherMngr {
 
    void foo();
    void bar();
}

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

This is new in beans.xml
<bean id="anotherMngr" class="biz.tugay.springaopexamples.manager.AnotherMngrImpl"/>

This is new in App.java
final AnotherMngr anotherMngr = applicationContext.getBean("anotherMngr", AnotherMngr.class);
anotherMngr.foo();
anotherMngr.bar();

This is new In Action
AnotherMngrImpl#foo
== After Advice Applied ==
AnotherMngrImpl#bar

Notes
AfterAdvice is applied just as we expected.. It was applied after the foo method which matches the package biz.tugay.springaopexamples.manager..