1. Preface
The first two blogs introduce the IOC container in spring, which explains the knowledge of AOP in spring.
2.AOP Basic Knowledge
2.1 Concepts
AOP is an aspect-oriented programming, a programming paradigm of software engineering. AOP is concerned with the functionality of the common features of the program, when developed, the common functions extracted from the production of separate modules, the original code will no longer have these extracted from the common function code. As a result, the reusability of the code is enhanced, and when the program is developed, it can only consider the personalization function, without considering the features of generality.
2.2 Basic points of knowledge
Connection points: Methods with specific functions, general methods
Entry point: A way of addressing a method that has a common function.
Target object: The class containing the Pointcut
Notice: The common function is extracted and made into a separate function module.
Facets: A case in which a pointcut matches a notification.
AOP Proxy: Run the process using AOP to create a proxy object to run, the extracted functionality is executed during the run, and the process is done automatically by AOP, so called an AOP proxy.
Weaving: The entire process of adding the extracted functionality to the original function is called weaving, and the weaving control is byte code
Work flow
At the time of development, the function class (target object) is produced, the common function (notification) of the method is extracted, The Independent class (notification Class) is made, and the method (Pointcut) in the original target object is no longer produced by the common function. After the feature is extracted, the complete business logic cannot be completed and the notification needs to be added to the corresponding location at runtime. In order to do this, you must set up a one-to-one correspondence between the pointcut and the notification class (tangent)
Run process
At run time, Spring always monitors the execution of the method corresponding to the pointcut configured in the slice, discovers that the method is executed, uses the AOP proxy mechanism, creates the proxy object (the AOP proxy), merges the original method (Pointcut) with the notification, and forms a complete business logic to run, this process is called weaving.
3.AOP Practice
3.1 Simple Demo
Making target objects
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//target object class public class UserService {//connection point//Pointcut public void Add () {//Commonality function was extracted and put into the FN method in the Myadvice class System.out.println ("Add"); Connection point public void Delete () {System.out.println ("bbbbbb"); System.out.println ("delete");}} </span></span>
Production Notification
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//Notice class Public classes Myadvice {//have common features: Notify public void fn () {//Commonality function System.out.println ("AAAAA");}} </span></span>
aopxml Configuration
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ SPRING-BEANS.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop.xsd "><!--Configuration notification class for Bean--><bean id=" Myadvice "class=" Com.aop.MyAdvice "></bean>< Bean id= "UserService" class= "com.aop.UserService" ></bean><!--Configure AOP--><aop:config><!-- Configure the Beanid--><aop:aspect for the notification class for the facet ref ref= Myadvice ><!--Configure notification classes and relationships (pointcuts and Notification methods)--><!--<aop:before Method= "FN" pointcut= "Execution (public void Com.aop.UserService.add ())"/>--><!--unqualified modifier--><!--< Aop:before MeThod= "FN" pointcut= "Execution (void Com.aop.UserService.add ())"/>--><!--unqualified return value--><!--<AOP: Before method= "FN" pointcut= "Execution (* Com.aop.UserService.add ())"/>--><!--<aop:before method= "FN" pointcut= "Execution (* *.aop. Userservice.add ()) "/>--><!--<aop:before method=" FN "pointcut=" Execution (* com.*. Userservice.add ()) "/>--><!--<aop:before method=" FN "pointcut=" Execution (* *: *. Userservice.add ()) "/>--><!--<aop:before method=" FN "pointcut=" Execution (* *: Userservice.add ()) "/>--><!--<aop:before method=" FN "pointcut=" Execution (* *: *service.add ()) "/>--><!--<aop:before method=" FN "pointcut=" Execution (* *: User*.add ()) "/>--><!--<aop:before method=" FN "pointcut=" Execution (* *: * * ()) "/>--><!--match a parameter--><!--<aop:before method=" FN "pointcut=" Execution (* *: * * (*)) "/>--><!--match two arbitrary parameters--><!--<aop:before method=" FN "pointcut= "Execution (* *). *. * (*,*)) "/>--><!--match any parameter--><!--<aop:before method=" FN "pointcut=" Execution (* *). *.*(..))" />--><!--match at least one parameter--><!--<aop:before method= "FN" pointcut= "Execution (* *). *.*(*,..))" />--><!--match the first parameter is an int, it doesn't matter--><!--<aop:before method= "FN" pointcut= "Execution (* *). * * (int,..)) " />--><aop:around method= "FN" pointcut= "Execution (* *). *.*(..))" /></aop:aspect></aop:config></beans></span></span>
Note: Pointcut is configured with pointcut expressions, POINTCUT-REF is configured as Pointcut reference objects, method configured by the Pointcut to execute the notification methods.
3.2 Notification Categories
Before: Pre-notification (application: various checks)
Executes before the method executes if an exception is thrown
After: Notification (application: cleaning site)
Executes after the method executes, regardless of whether an exception occurs in the method
Afterreturning: Notification after return (application: General data Processing)
Method executes after normal return and cannot be executed if an exception is thrown in the method
Afterthrowing: Notification after throwing exception (application: Packing exception information)
Method throws an exception and executes if the method does not throw an exception that cannot be executed
Around: Surround notification (app: Very powerful, can do anything)
Methods are executed before and after execution, which prevents the execution of the method
Surround notification public void around (Proceedingjoinpoint pjp) throws Throwable{system.out.println ("around before ...");// Call the original method Pjp.proceed (); System.out.println ("Around after ...");
1. Configuration format for notifications
<aop:before pointcut-ref= "Pt2" method= "before"/> <aop:after pointcut-ref= "pt2" method= "after"/> <AOP: After-returning pointcut-ref= "pt2" method= "afterreturning"/> <aop:after-throwing pointcut-ref= "pt2" method= " Afterthrowing "/> <aop:around pointcut-ref=" pt2 "method=" Around "/>
2. Notification Order: Related to Configuration order
Multiple slices between
The first declared before run first,
After declaring the before to run after
First declared after-run
After the declared after run first
Summary: Configuration is based on the final run order
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Learning Note III (those things in AOP)