Spring03-- a summary on the Spring AOP

This article will bring you another focus of knowledge of Spring - Spring AOP. I am concerned about the number of public "side Java Code" 10:24 every day with you to learn more Java-related knowledge.

What is AOP

Aspect Oriented Programming (aspect-oriented programming), a crosscutting concerns of business logic programming. Each crosscutting concerns are concentrated in one place, rather than scattered in several places in the code. Such modules to make our services more concise, because they contain only the main concern of the code, while the secondary functions or auxiliary functions are transferred to the section in the.

AOP main scenarios are:

  1. Authentication Permissions
  2. Caching Cache
  3. Context passing content delivery
  4. Error handling Error handling
  5. Lazy loading lazy loading
  6. Debugging Debugging
  7. logging, tracing, profiling and monitoring recording tracks Picture
  8. Performance optimization performance optimization
  9. Persistence Persistence
  10. Resource pooling resources pool
  11. Synchronization Synchronization
  12. Transactions Transaction

AOP core knowledge points

Key Terms

  • Aspect (cut) : cut a separate module business processes, in one application can have any number of facets. Such as transaction management, it is a section of the application example;
  • Join point (connection point) : the business process needs to be inserted during operation in the specific location of the cut surface. As a particular method or process performed when abnormality;
  • Advice (notification) : a specific method facets. Can be divided into pre-notification (Before), post-notification (AfterReturning), abnormality notification (AfterThrowing), the final notification (the After) and surround notification (Around) are five. Specific implementation are the types notification is specified in the configuration file and annotations;
  • Pointcut (entry point) : used to define notifications which should be cut to the point of attachment, different notifications typically requires cutting to different connection points;
  • Target (target object) : object is notified or a plurality of facets;
  • Proxy (proxy object) : After the object to the target object is dynamically created will notify the application. As can be easily understood, the business logic for the proxy object function of the target object together with object incised section formed;
  • Weaving (cut) : the section applied to the target object is to create a new proxy object of the process. This process can occur at compile time, the class loader and Operating Periods.

Notification type

Spring AOP There are five main types of notifications, which are pre-notification (the Before), post-notification (afterReturning), abnormality notification (AfterThrowing), the final notification (the After) and surround notification (Around):

  • Before (pre-notification) : Advice to be executed before connecting point, but which does not prevent the flow of execution before the connection point (unless it throws an exception);
  • AfterReturning (Rear notification) : notification after completion of the connection point is normally performed;
  • AfterThrowing (abnormality notice) : Notify exception is thrown when executing method at the connection point;
  • After (final notice) : After the completion of the notification (either completed normally or with an exception to withdraw) performed at the connection point;
  • Around (around advice) : The notification may be performed both before and after the connection point of execution.

Two AOP proxy mode

Spring provides two ways to generate a proxy object: JDKProxy and Cglib, generated according to the configuration determined by AopProxyFactory AdvisedSupport object which way the specific use. The default strategy is if the target class is an interface, use JDK dynamic proxy technology, otherwise use Cglib to generate a proxy.

JDK dynamic interface agent

JDK dynamic agent mainly related to two classes java.lang.reflect package: Proxy and InvocationHandler. InvocationHandler is an interface defined by transverse logic implementing this interface, and code for the target class invoked by reflection, dynamic crosscutting preparation together and business logic. Proxy use InvocationHandler dynamically create an instance of a line with an interface to generate proxy object target class.

CGLib dynamic proxy

CGLib called the Code Generation Library, is a powerful, high-performance, high-quality code generation libraries, Java classes can extend and implement Java interfaces, CGLib asm encapsulates at runtime, dynamically generate a new run of the class. JDK dynamic proxies and compared: JDK there is a limit to create a proxy, proxy instance is created only for the interface, but not for business methods defined by the interface of the class, you can create a dynamic proxy by CGLib.

Spring Series Recommended

Spring02 - Spring Bean in the life cycle and its scope

Spring01-- everything you should know, about the IOC container

Guess you like

Origin www.cnblogs.com/weechang/p/12587431.html