Review the five notification notes
Use demo
1. Facet class (@ Aspect)
@Aspect//Indicates that it is a faceted class public class MyAspect { //Extract common pointcut expressions //1. Reference of this class: write method name directly () //2. Other facet References: the full class name of the method () @Pointcut("execution(public int com.math.calculator.*(..))") public void point(){}; //Before the target method, the pointcut expression (specifying which method to cut into) //Demonstration of tangent point expression referenced by this class of methods: @Before("point()") public void logStart() { System.out.println("Before the target method runs..."); } //Demonstration of tangent point expression of external method reference: @After("com.aop.MyAspect.point()") public void logAfter() { System.out.println("After the end of the target method..."); } @AfterReturning("point()") public void logReturning() { System.out.println("After the target method returns normally..."); } @AfterThrowing("point()") public void logThrowing() { System.out.println("After an exception occurs in the target method..."); } }
Business logic class
//Calculator class public class calculator { public int add(int i,int j) { return i+j; } }
Add aspect classes and business logic classes to the container in the configuration class
Remember to add @ EnableAspectJAutoProxy to the configuration class to enable the annotation based aop mode
@EnableAspectJAutoProxy @Configuration public class MyConfig { @Bean public MyAspect myAspect() { return new MyAspect(); } @Bean public calculator calculator() { return new calculator(); } }
test
public class Main { //The location of the configuration class is passed in. At the beginning, the configuration class is loaded, and before that, the location of the configuration file is loaded private static AnnotationConfigApplicationContext ioc= new AnnotationConfigApplicationContext(MyConfig.class); public static void main(String[] args) { calculator bean = ioc.getBean(calculator.class); System.out.println(bean.add(1,2)); } }
Note: many Enablexxx in spring enable a certain function
Get the relevant information of the cut in method in the method of the cut in class
The joinpoint must appear in the first place of the method parameter
@Aspect//Indicates that it is a faceted class public class MyAspect { //Extract common pointcut expressions //1. Reference of this class: write method name directly () //2. Other facet References: the full class name of the method () @Pointcut("execution(public int com.math.calculator.*(..))") public void point(){}; //Before the target method, the pointcut expression (specifying which method to cut into) //Demonstration of tangent point expression referenced by this class of methods: @Before("point()") public void logStart(JoinPoint joinPoint) { System.out.println("Before the target method runs "+"Method name:"+joinPoint.getSignature().getName()); } //Demonstration of tangent point expression of external method reference: @After("com.aop.MyAspect.point()") public void logAfter() { System.out.println("After the end of the target method..."); } //The joinpoint must appear in the first place of the method parameter @AfterReturning(value = "point()",returning = "res") public void logReturning(JoinPoint js,Object res) { System.out.println(js.getSignature().getName()+"After the method returns normally "+"Method return value:"+res); } //The joinpoint must appear in the first place of the method parameter @AfterThrowing(value = "point()",throwing = "ex") public void logThrowing(JoinPoint js,Exception ex) { System.out.println(js.getSignature().getName()+"After the method has an exception "+"The exception is:"+ex); } }
For detailed usage of JoinPoint object, refer to the following article
Usage of JoinPoint object in SpringAop
AOP annotation development Trilogy
1. Add business logic components and aspect classes to the container and tell the spring container which is the aspect class (@ Aspect)
2. Mark the notification annotation on each notification method on the aspect class to tell Spring when and where to run (pointcut expression)
3. Enable annotation based aop mode
AOP principle analysis part
AOP principle [see what component is registered in the container, when the component works, and what the function of the component is]:
@EnableAspectJAutoProxy:
==>@ import ({aspectjautoproxyregister. Class}): import aspectjautoproxyregister into the container
==>Use aspectjauto proxyregistrar to customize the registration of bean s in the container
==>Register an annotation awareaspectjautoproxycreator (AspectJ automatic proxy creator) in the container
The following shows the parent class hierarchy of AspectJ automatic proxy Creator:
AnnotationAwareAspectJAutoProxyCreator
–>AspectJAwareAdvisorAutoProxyCreator
—>AbstractAdvisorAutoProxyCreator
—>AbstractAutoProxyCreator
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
Post processor, automatic assembly of BeanFactory
The process of creating and registering AnnotationAwareAspectJAutoProxyCreator:
1. Pass in the configuration class and create an ioc container
2. Register the configuration class and call refresh() to refresh the container
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); this.register(annotatedClasses);//Register configuration class this.refresh();//Refresh container }
3. In the refresh method = = > this registerBeanPostProcessors(beanFactory);
Register the post processor of the bean to facilitate the creation of intercepting beans
3.1 Get first ioc All defined objects in the container that need to be created BeanPostProcessor 3.2 Add something else to the container BeanPostProcessor 3.3 Priority registration is realized PriorityOrdered Interfaced BeanPostProcessor 3.4 Then register the implementation in the container Ordered Interfaced BeanPostProcessor 3.5 The priority interface is not implemented BeanPostProcessor 3.6 register BeanPostProcessor,It's actually creating BeanPostProcessor Save in container establish internalAutoProxyCreator of BeanPostProcessor[AnnotationAwareAspectJAutoProxyCreator] (1).establish bean Examples of (2).populateBean:to Bean Assignment of various attributes of (3).initializeBean:initialization Bean (3.1).invokeAwareMethods(): handle Aware Method callback of interface (3.2).applyBeanPostProcessorsBeforeInitialization:Application of post processor postProcessBeforeInitialization (3.3).invokeInitMethods():Execute custom initialization methods (3.4).applyBeanPostProcessorsAfterInitialization Execute post processor postProcessAfterInitialization (4).BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)Created successfully
Summary: if we need to enhance a business logic component, the business logic component we finally get from the container is actually an enhanced proxy object
summary