Implementation of AOP based on agent class
- In spring, JDK dynamic proxy is used to realize AOP programming by default. Using proxy class to create proxy is the most basic way to realize spring AOP
- According to the location of the connection point of the notification in the target method in Spring, the notification can be divided into the following six types
- 1 - surround notification: enhancements are implemented before and after the execution of the target method, which can be used for logging and transaction processing.
- 2 - pre notification: the enhancement is implemented before the implementation of the target method, which can be applied to functions such as permission management.
- 3 - post return notification: implement enhancement after the target method is executed, which can be used to close the flow, delete temporary files, etc.
- 4 - post final notification: after the execution of the target method, the enhancement is implemented. This kind of notification must be executed regardless of whether an exception occurs. This kind of notification can be used to release resources.
- 5 - exception notification: enhanced after the method throws an exception, which can be used to handle exceptions, record logs and other functions.
- 6 - Import notification: add some new methods and attributes to the target class, which can be applied to modify the target class.
- ProxyFactoryBean is an interface implementation class. FactoryBean is responsible for instantiating a Bean instance, and ProxyFactoryBean is responsible for creating proxy instances for other beans.
- Common properties of ProxyFactoryBean class:
- 1-target: the target object of the proxy
- 2-proxyInterfaces: the interface that the agent needs to implement
- 3-interceptorNames: Advice that needs to be woven into the target
- 4-proxyTargetClass: whether to proxy the class instead of the interface. The default is false. JDK proxy is used. When true, CGLIB proxy is used
- 5-singleton: whether the returned proxy instance is a singleton. The default value is true
- 6-optimize: when set to true, CGLIB proxy is forced to be used
The development environment used this time is IDEA
The process of Spring creating AOP proxy using ProxyFactoryBean is demonstrated through an example of surround notification
1 - use IDEA to create a Spring project. Choose DownLoad for Libraries. DownLoad the required jar package automatically without importing
The jar packages imported automatically by default are as follows:
2 - create the package dynamic. In src directory JDK, and create the interface TestDao and the implementation class TestDaoImpl
public interface TestDao { public void save() ; public void modify() ; public void delete() ; }
public class TestDaoImpl implements TestDao { @Override public void save() { System.out.println("preservation") ; } @Override public void modify() { System.out.println("modify") ; } @Override public void delete() { System.out.println("delete") ; } }
3 - create the package spring.com in the src directory Proxyfactorybean package and create the facet class MyAspect in the package
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; //The process of Spring creating AOP proxy using ProxyFactoryBean is demonstrated through an example of surround notification public class MyAspect implements MethodInterceptor { public void check(){ System.out.println("Simulation permission control") ; } public void except(){ System.out.println("Simulated exception handling") ; } public void log(){ System.out.println("Simulation logging") ; } public void monitor(){ System.out.println("Monitoring performance simulation") ; } @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { //Override invoke interface method //Pre enhancement check() ; except() ; //Execution target method Object object = methodInvocation.proceed() ; //Post enhancement log() ; monitor() ; return object; } }
4 - configure the bean instance for the aspect class, which is the aspect object recognized by the Spring container
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <!--Define target object--> <bean id = "testDao" class = "dynamic.jdk.TestDaoImpl"/> <!--Create a slice--> <bean id = "MyAspect" class = "spring.proxyfactorybean.MyAspect"/> <!--use Spring The agent factory defines a named testDaoProxy Proxy object for--> <bean id = "testDaoProxy" class = "org.springframework.aop.framework.ProxyFactoryBean"> <!--Specifies the interface implemented by the proxy--> <property name="proxyInterfaces" value = "dynamic.jdk.TestDao"/> <!--Specify target object--> <property name = "target" ref = "testDao"/> <!--Specify the section and weave in the circular notice--> <property name = "interceptorNames" value = "MyAspect"/> </bean> </beans>
5 - in Spring Create the test class ProxyFactoryBeanTest in the proxyfactorybean package, use the Spring container in the main method to obtain the enhanced target object and execute the target method.
import dynamic.jdk.TestDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Create a test class, use the Spring container in the main method to obtain the enhanced target object and execute the target method */ public class ProxyFactoryBeanTest { public static void main(String[] args){ //Initialize the Spring container and load the configuration file ApplicationContext appCon = new ClassPathXmlApplicationContext("spring-config.xml") ; //Get enhanced target object from container TestDao testDaoAdvice = (TestDao) appCon.getBean("testDaoProxy"); //Execution method testDaoAdvice.save() ; System.out.println("================") ; testDaoAdvice.modify() ; System.out.println("================") ; testDaoAdvice.delete() ; System.out.println("================") ; } }
6 - test results into the following: