1 , AOP (plane-oriented programming)
AOP is an abbreviation for Aspect oriented programming , meaning: plane-oriented programming
A technique to realize the unified maintenance of program function by pre-compiling and running-time dynamic agent
AOP is a continuation of OOP, a hotspot in software development, an important content in the spring framework, and a derivative model of functional programming.
AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.
AOP ideas: Horizontal Repetition, Portrait extraction
2 , bottom-level implementations: The bottom of Spring's AOP uses two kinds of proxy mechanisms:
Dynamic agent for JDK: Generating proxies for classes that implement interfaces
Cglib Dynamic Proxy: Generates proxies for classes that do not implement interfaces, and uses underlying bytecode enhancement techniques to generate subclass objects of the current class
3and the related terminology in the development of AOP:
Joinpoint (Connection point): The so-called connection point refers to the points that are intercepted. In spring, these points refer to methods because spring only supports connection points for method types (points that can be cut in) Pointcut (pointcuts): The so-called pointcut refers to the definition of which joinpoint we want to intercept. (points that have been cut) Advice (notification/ enhancement): The so-called notification refers to the interception to Joinpoint after the thing to do is to notify. Notification is divided into pre-notification, post-notification, exception notification, final notification, surround notification (section to complete function) Introduction (Introduction): Introduction is a special kind of notice without modifying the class code, Introduction can dynamically add some methods to the class at run time or Field. Aspect (tangent): a combination of a pointcut and a notification (the target object): The target object of the proxy (proxy): When a class is woven into an AOP, a result proxy class is generated. Weaving (weaving): refers to the process of applying an enhancement to a target object to create a new proxy object. Spring uses dynamic proxy weaving, while the AspectJ is woven into the compile-time and class-loading stages.
4. The project structure of the example:
4.1 Using XML configuration
<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/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!--target Object--<bean name= "UserService"class= "Com.roxy.spring.service.UserServiceImpl" ></bean> <!--Notification Object--<bean name= "Transactionadvice"class= "Com.roxy.spring.advice.TransactionAdvice" ></bean> <!--to weave the notification object into the target object--<aop:config> <!--Select Pointcuts--<aop:pointcut expression= "Execution (* com.roxy.spring.service. *serviceimpl.* (..)) " Id= "Pointcut"/> <!--Select Cut-In time-<aop:aspect ref= "Transactionadvice" > <aop:before method= "before" pointcut-ref= "Pointcut"/> <aop:after-returning method= "afterreturning" pointcut-ref= "Pointcut"/> <aop:after method= "after" pointcut-ref= "Pointcut"/><!--call-<!--<aop:arou Whether or not an exception occurs nd method= "Around" pointcut-ref= "Pointcut"/> <aop:after-throwing method= "afterexception" pointcut-ref= "PO Intcut "/> </aop:aspect> </aop:config></beans>
Public Interface UserService { publicvoid Save (); Public void Delete (); Public void update (); Public void select ();}
Public classUserserviceimplImplementsuserservice{@Override Public voidSave () {System.out.println ("Save User"); } @Override Public voidDelete () {System.out.println ("Delete User"); } @Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("Update User"); } @Override Public voidSelect () {System.out.println ("Query User"); }}
Public classTransactionadvice { Public voidbefore () {System.out.println ("Pre-notification is executed"); } Public voidafterreturning () {System.out.println ("Post notification is executed (exception not called)"); } Public voidAfter () {System.out.println ("The post notification is executed (whether or not an exception is called)"); } Public voidafterexception () {System.out.println ("Exception notification is executed"); } PublicObject around (Proceedingjoinpoint point)throwsthrowable {System.out.println ("Around_before"); Object Proceed= Point.proceed ();//calling the target methodSystem.out.println ("Around_after"); returnproceed; }}4.2 Using annotations
<!--target Object-- class= "Com.roxy.spring.service.UserServiceImpl" ></bean> <!-- Notification Object- -class= "Com.roxy.spring.advice.TransactionAdvice" ></bean> <!--to turn on weaving annotations-- > <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Aspect //This class is tangent Public classTransactionadvice {@Pointcut (value= "Execution (* com.roxy.spring.service). *serviceimpl.* (..)) ") public void PointCut () {}@Before ( "Transactionadvice.pointcut ()") Public voidbefore () {System.out.println ("Pre-notification is executed"); } @AfterReturning ( "Transactionadvice.pointcut ()") Public voidafterreturning () {System.out.println ("Post notification is executed (exception not called)"); } @After ( "Transactionadvice.pointcut ()") Public voidAfter () {System.out.println ("The post notification is executed (whether or not an exception is called)"); } @AfterThrowing ( "Transactionadvice.pointcut ()") Public voidafterexception () {System.out.println ("Exception notification is executed"); } @Around ( "Transactionadvice.pointcut ()") PublicObject around (Proceedingjoinpoint point)throwsthrowable {System.out.println ("Around_before"); Object Proceed= Point.proceed ();//calling the target methodSystem.out.println ("Around_after"); returnproceed; }}
5. Order of 5 notification calls
Console
Around_before
Pre-notification is executed
Save user
Around_after
The post notification is executed (whether or not an exception occurs)
Post notification is executed (exception not called)
Result
Surround notification--front notification--call method-surround notification--post-notification (after)--post-notification (afterexception)
6. Regular Expressions in AOP
AOP configures a regular expression for pointcuts. Match all single characters except line break * match preceding character 0 or more times + match preceding character 1 or more times? Match the preceding character 0 or 1 times ^ Match string start $ Match string end x| Y match x or y[xyz] Zyx where one {n} matches n times {n,} matches at least n times {n,m} matches n times to M times \ Transpose characters
Spring Frame--AOP