Spring AOP understanding the nature of technology

Spring AOP understanding the nature of technology


A, AOP Introduction

  AOP (Aspect Oriented Programming, Aspect Oriented Programming), to a certain class of problems in one place for processing, such as the click event handler, print logs.

1、Join Points:

  Referred JPoints, AspectJ is the most critical concept, it expressed some point in the program execution runtime. In theory, a program in many places can be seen as JPoint, but AspectJ, only a few execution point is considered JPoints, such as the construction method calls, method calls, method of execution, exception and so on. JPoints actually trying to represent AspectJ code into programs which place, is inserted in the method, or inserted before and after the method call. It should be noted: In AspectJ, method invocation (call) and methods of execution (execution) is not the same, do this later introduction.

2、Pointcuts

  A program will be a lot of JPoints, even the same function, is also divided into call type and execution of JPoint type, but not all are JPoint we need to be concerned. For example, we may only need to be concerned about the click event method, then we are interested in how to choose JPoint from many JPoints in it? This time can be used Pointcut.

3、Advice

  Advice is simply represent hook points AspectJ in AspectJ is commonly used before, after, around and so on. Before JPoint representation before execution, we need to do. after after JPoint represents the execution, around before and after the execution is represented in JPoint.

4、Aspect

  Earlier we talked about in the course of AspectJ need to use a concept to deal with the problem need to be unified in one place to deal with, this place is Aspect, meaning "cut." In Java development primarily using @Aspect annotation to represent a section.


Two, AspectJ Introduction

  AspectJ is an aspect-oriented programming framework that extends the Java language. AspectJ AOP defines the syntax , it has a special compiler to generate Java byte code to comply with Class file specification. AspectJ also supports native Java, AspectJ can only add comments to offer.

  AspectJ AOP to achieve results, the key lies in the realization of AOP AOP proxy AOP framework created automatically, AOP proxy can be divided into static and dynamic proxy agent two major categories:

  Static agent is the use of commands provided by the framework AOP compiler, thereby generating the proxy class through the AOP AOP framework instructions at compile time, it is also known to enhance compile ; there is a static agent is achieved without tools to write code; this general manner proxy mode to use.

  Dynamic agent is at runtime by means of JDK dynamic proxies, CGLIB other "temporary" AOP generate dynamic proxy class, it is also known in memory enhancement runtime .

  Enhanced dependent AOP, the POM-based AspectJ compile native AspectJ not depend on Spring case, it is enhanced for AOP compile bytecode modification, enhancement function based on the compiler AspectJ; does not generate a new proxy class bytecode.

  JDK dynamic proxy implementation: https://github.com/cyneck/demo/blob/master/src/main/java/com/example/demo/aopJdk/testAopJDKProxy.java

  CGLIB dynamic proxy implementation: https://github.com/cyneck/demo/blob/master/src/main/java/com/example/demo/aopCglib/testAopCglibKProxy.java


Third, the rapid use of Spring aop

  Show my code:https://github.com/cyneck/demo/blob/master/src/main/java/com/example/demo/aop/log/LogAspect.java

  This example is an example of a section of log, in the development process, it is used in the main spring provided @Aspect annotation implemented section. Similar comments also global exception unitary @ControllerAdvice (the annotation, mainly captured from the final thrown exception Controller layer, belongs to the enhancement layer of the Controller.), Often used in combination to handle @ExceptionHandler thrown exception processing logic.

  ControllerAdvice annotation is declared in the class, which uses three main points:

  • The method of binding annotation type @ExceptionHandler, for capturing the specified type of exception thrown Controller, so as to achieve the difference between different types of exception process;
  • @InitBinder annotation type bonding method, the request for registering the custom parameter analytical methods, so as to achieve the specified custom format parameter;
  • Combined method type annotation @ModelAttribute, marked its representation will be executed before the target Controller method is executed.

Fourth, expand: a dynamic agent

  1, jdk dynamic proxy and proxy cglib dynamic difference:

  • jdk dynamic proxy class is generated anonymous implement a proxy interface using reflection, InvokeHandler calls before calling a specific method to process.

  • asm cglib dynamic proxy using open source packages, class files proxy object classes loaded in, processed by modifying a subclass bytecode.

  • If the target object implements the interface, it will use JDK dynamic proxy AOP implementations default

  • If the target object implements the interface, you can force the use of CGLIB achieve AOP

  • If the target object does not implement the interfaces must be used CGLIB library, spring will automatically between JDK dynamic proxies and convert CGLIB

  2, force the use of CGLIB achieve AOP

  (1) adding CGLIB library, SPRING_HOME / CGLIB / * JAR;.
  (2) was added in the spring profile

<aop:aspectj-autoproxy proxy-target-class="true"/>

  3, JDK dynamic proxy and bytecode generation CGLIB distinction
  (1) JDK dynamic proxy only for the class implements an interface generation agent, and not for the class;
  (2) CGLIB agent is achieved for the class, the main class is specified generating a sub-class, wherein the method is covered, as is inheritance, class or method is best not to be declared as final.


Guess you like

Origin www.cnblogs.com/aric2016/p/11497332.html