ProxyFactory, AspectJProxyFactory and ProxyFactoryBean are all inherited from proxycreationsupport classes. Through these three Spring framework classes, dynamic proxy objects can be obtained. Among them, ProxyFactory class supports MethodBeforeAdvice, afterreturnadvice, MethodInterceptor and ThrowsAdvice This blog explains how to use ProxyFactory class to realize pre enhancement and post enhancement
Concept:
The pre enhanced class must implement the MethodBeforeAdvice interface, which is executed before the target class method is executed; the post enhanced class must implement the afterreturnadvice interface, which is executed after the target class method is executed.
Scene:
Enterprises invite stars to sing or directors invite stars to shoot movies or TV plays. Generally, they consult with their agents first, and then the agents inform the stars. After the stars agree, they perform. Finally, the agents deal with the aftermath. However, some stars do not have an economic person. In this case, the enterprises or directors can directly find the stars for consultation, and negotiate to complete the performance A fee will be charged.

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice{

	/**
	 * Preamplifier
	 * 
	 * @author GaoHuanjie
	 */
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		if("sing".equals(method.getName())){
			System.out.println("Negotiate with the customers to determine the performance time!");
			System.out.println("Negotiate with customers to determine the venue!");
			System.out.println("Negotiate with customers to determine security measures!");
			System.out.println("Negotiate with the customers to determine the amount of exit fee!");
		} else if("play".equals(method.getName())) {
			System.out.println("Negotiate with the director to determine the role!");
			System.out.println("Negotiate with the director to determine the shooting time!");
			System.out.println("Negotiate with the director to determine the shooting location!");
			System.out.println("Negotiate with the director to determine security measures!");
			System.out.println("Negotiate with the director to determine the pay!");
		}
	}
}
import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvice implements AfterReturningAdvice{

	/**
	 * Post enhancement
	 * 
	 * @author GaoHuanjie
	 */
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		if("sing".equals(method.getName())){
			System.out.println("Collect the customer's appearance fee!");
		} else if("play".equals(method.getName())) {
			System.out.println("Collect the director's remuneration!");
		}
	}
}
public interface IWork {

	/**
	 * Singing aspect
	 * 
	 * @author GaoHuanjie
	 */
	void sing();
	
	/**
	 * In terms of performance (e.g. film making, TV Series)
	 * 
	 * @author GaoHuanjie
	 */
	void play(int option);
}
public class Star implements IWork{
	
	private String name;//Name
	
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Singing, very talented, beautiful voice
	 * 
	 * @author GaoHuanjie
	 */
	@Override
	public void sing() {
		try {
			System.out.println(name+"Singing......");
			Thread.sleep(10000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * In terms of performance (e.g. film making, TV Series), it is lifelike and vivid
	 * 
	 * @author GaoHuanjie
	 */
	@Override
	public void play(int option) {
		try {
			switch (option) {
				case 1:
					System.out.println(name+"Making a movie......");
					break;
				case 2:
					System.out.println(name+"On TV......");
			}
			Thread.sleep(30000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
import org.springframework.aop.framework.ProxyFactory;

public class Test {

	public static void main(String[] args) {
		Star star = new Star();
		star.setName("Lau Andy");
		System.out.println("A Monitoring points:"+star);
		ProxyFactory proxyFactory = new ProxyFactory();//Create agent factory
		proxyFactory.setTarget(star);//Set target class
		proxyFactory.addAdvice(new BeforeAdvice());//Add pre enhancement
		proxyFactory.addAdvice(new AfterAdvice());//Add post enhancement

		proxyFactory.setProxyTargetClass(false);//Since the proxyFactory object does not call setInterfaces method to set the proxy interface, even if it is set to false here, Spring still uses CGLib library to generate proxy object
		
		IWork agent = (IWork) proxyFactory.getProxy();//Get enhanced Star object from agent factory
		System.out.println("B Monitoring points:"+agent);//Same as the output of monitoring point A
		System.out.println(agent.getClass());//Output: class Star$$EnhancerBySpringCGLIB$a65142
		agent.sing();//Call proxy method
		
		System.out.println("##########################################################################");
		
		agent.play(1);//Call proxy method
	}
}