Aspect Oriented Programming (Aspect Oriented Programming) is a software programming ideas developed to a certain stage of the product that complements object-oriented programming. AOP generally suitable for applications having a transverse logic, such as access control, transaction management, performance testing and the like.
Logs, exception handling, transaction control, etc. are a robust business system requires. However, in order to ensure robust systems available, they must re-write many business methods similar code repeatedly, making an already very complex code becomes more complex business processes. Business functionality developers have to test these two additional code if handled correctly, if there is omissions, or if you need to change the format of the log information to verify security rules, or to add auxiliary functions, will lead to a large number of frequent business code Modifications.
Aspect Oriented Programming, that is, without changing the code to add new features on the basis of the original program, the code section enhancement processing. His design ideas from the proxy design pattern.
1. Before inserting the original object enhancement processing method for the front reinforcement
2. The method of executing the subsequent insertion of enhanced post-enhancement processing
3. The method of enhancement around the front and rear surround processing is enhanced, is the most powerful enhancement processing, can obtain or modify the parameters of the target method, the return value, exception handling, and even decide whether to perform target method.
4. The enhancement processing method throws an exception when an exception is thrown enhanced
The final enhancement processing, regardless of method throws an exception or normal exit will be implemented, similar to the role of exception handling mechanism in the finally block, generally used for releasing resources
Use annotations on the Springboot
Need to introduce the required jar: spring-boot-starter-aop
Create a aop enhancement processing class
SLF4J @ @Aspect @Component public class LoggerAspect { // All methods com.lzz.lzzapp.common.user the matching packets and sub-packets of all classes @Pointcut ( "execution (* com.lzz.lzzapp.common.user .. *. * (..)) " ) public void logPointCut () { } // preamble enhanced notification performed before the connection point execution @Before (" logPointCut () " ) public void before () { log.info ( " about to call a business method " ); } // ultimately enhancing notification is performed after the connection point of execution (notification and exception abnormality notification) @After (" logPointCut () " ) public void after () { log.info ("Method call completion service" ); } / ** * notification (notification return) Rear enhanced executed after executing the connection point * / @AfterReturning (value = "logPointCut ()", returning = "Result" ) public void doAfterReturning (the JoinPoint Joinpoint, Result Object) { String methodName = . joinPoint.getSignature () getName (); log.info ( "call" + joinPoint.getTarget () + "a" + methodName + ". method parameters:" + of Arrays.toString (joinPoint.getArgs ()) + "method returns the value:." + Result); } / ** * abnormal enhancement of notification performed after performing a connection point (abnormality notice) * / the @AfterThrowing ( "logPointCut ()") public void doAfterThrowing () { log.info ( "exception handling is completed" ); } / ** * Enhanced Surround * / @Around ( "logPointCut ()" ) public void doAround (JP ProceedingJoinPoint) { log.info ( "call" + jp.getTarget () + "a" + jp.getSignature () getName () + " method parameters:.". + of Arrays.toString (jp.getArgs ())); the try { Object Result = jp.proceed (); // perform certain method log.info ( "method return value:" + Result); } the catch (the Throwable E) { log.error (jp.getSignature ().getName () + "method of detection") e.printStackTrace (); } } }
Use @Aspect definitions section, @ Pointcut defined entry point
Pointcut matching is performed to the point of attachment points JointPoint, Spring automatically injected into the instance () method getTarget joinpoint proxy object can be obtained, getSignature () method returns the target method is the agent. as getArgs () method returns an array of arguments passed to the target method
For rear reinforcement may also define a proxy for receiving the return value of the method, must @AfterReturning by annotations returning the name of the parameter specified property
execution is the entry point indicator, his expression in parentheses an entry point, you can configure the method to be cut, the entry point expression support fuzzy matching
public * addUser (com.entity.User) * represents the return value matches all types of
public void * (com.entity.User) * which matches all method names
public void the addUser (..) .. represents the number of all parameters match and type
* com.user. *. * (..) represents all the methods of all classes packet matches com.entity * com.user .. *. * (.. ) represents all the sub-packets and packets matching com.entity All methods of the class