ProxyFactoryBean example!

In other words, lets modify the previous example!

App.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
    public static void main(String[] args) {
        final ClassPathXmlApplicationContext classPathXmlApplicationContext
                = new ClassPathXmlApplicationContext("beans.xml");
        final MyService myProxyFactory =
                (MyService) classPathXmlApplicationContext.getBean("myProxyFactory");
        myProxyFactory.serve();
    }
}

beans.xml (for first version)
<?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="myService" class="MyServiceImpl"/>
    <bean id="myMethodBeforeAdvice" class="MyMethodBeforeAdviceImpl"/>
 
    <bean id="myProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="interceptorNames">
            <list>
                <value>myMethodBeforeAdvice</value>
            </list>
        </property>
        <property name="target" ref="myService"/>
    </bean>
</beans>

beans.xml (for second version)
<?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="myService" class="MyServiceImpl"/>
    <bean id="myMethodBeforeAdvice" class="MyMethodBeforeAdviceImpl"/>
 
    <bean id="myNameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="myMethodBeforeAdvice"/>
        <property name="mappedName" value="serve"/>
    </bean>
 
    <bean id="myProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="interceptorNames">
            <list>
                <value>myNameMatchMethodPointcutAdvisor</value>
            </list>
        </property>
        <property name="target" ref="myService"/>
    </bean>
</beans>

Wait a minute! InterceptorNames takes either an Advice or an Advisor?

Yes! Exactly!
Set the list of Advice/Advisor bean names. This must always be set to use this factory bean in a bean factory.