Spring AOP knowledge consolidation

Spring AOP by more than a month to learn to master the basic concepts of Spring AOP. AOP is the aspect-oriented programming (Aspect-Oriented Programming), it is based of OOP (object oriented programming, Object-Oriented Programming) architecture developed a set of procedures.
Under micro-services architecture system, sometimes a number of modules required for some commonality of affairs in the software, such as logging module, external business logic. To log module, for example, according to the thinking of OOP, I can build a log class and class in the class initialization log every need logging, logging purposes. The disadvantage is that you have all the business code that will be used in logging class is instantiated, and then execute the method log class. If the idea of using AOP, the log can be used as class section, and then use a proxy way this logging class need to cut logging class. Execution log just write a class method, the code is easier to maintain.
During the practice, the idea of using Spring AOP, adding the logging module is PressSystem project. MySQL database tables using log records, the database connection using MyBatis.

The basic concepts of AOP

AOP the following basic concepts are given after their own understanding. If you want to see the original, you can click on the Spring AOP official document end of this document.

 

  • Aspect (section): a modular mechanism for cutting a plurality of classes. In Spring AOP, aspect implemented in two ways:
    • Conventional class, and then use the XML Schema-based way
    • Conventional class, and then add @Aspect notes, that is the way @AspectJ

 

  • JoinPoint (connection points): can be understood as a method, the method is that the section to be cut.

  • Advice (notification): notification method is cut in, this method will JoinPoint cut.

  • Pointcut (cut-off point): cut-off point is the expression, in line with the notification will cut JoinPoint point cut in the expression.

  • Introduction (introduction): introducing mechanism to the subject invention is a new interface that can be cut, and to achieve it. In fact, it is to be cut into the cast object into another class, another class so that the method can be performed.

  • Target Object (target audience): the object is to be cut into. Spring AOP, may be the target object is always the proxy object.

  • AOP Proxy (AOP proxy): AOP Proxy is generated by an object out of the AOP framework, this object implements section of function, is to be cut after the target class results. Spring AOP proxy in two ways:

 

    • JDK dynamic proxies
    • CGLIB agent

 

  • Weaving (weaving): during the weaving section is connected with the target class. A product of the process is to be cut into the goal.

 

Type of notification

Simply speaking, the notification can inform front, rear notification, notification, and surround. For example, pre-notification is to inform the Executive before the entry point for execution; after advice is to execute the entry point after the execution. Spring AOP in my sample program, cited the implementation of the following notification.

  • Before advice (pre-notification): executed before the connection point. Unless it is before advice throws an exception, otherwise there is no ability to influence the execution flow to the connection point before advice.

  • After returning advice (RETURN notification): After the notification method of connecting points normally been performed and then executed. So-called normal executed, for example, the connection point method does not throw an exception.

  • After throwing advice (throws after notification): If the method throws an exception because of the termination, then the notification execution thrown.

  • After (finally) advice (after advice): how to exit the case regardless of the method of connection points, is normal or abnormal, after advice will be executed.

  • Around advice (notification Surround): Surround notification is surrounded join point, the method may be performed in a custom front of or behind the point of attachment. Surround notification may decide whether to perform the method of connection points, or a method of bypassing the connection point. Bypassing way around advice is to return their value or throw an exception.

PressSystem project basic components of Spring AOP

Components mentioned below, Spring AOP is to achieve minimal subset of components.

  • XuanTiController.java
    in PressSystem project, the user is the jsp page, jsp page will send an asynchronous request to the XuanTiController, in XuanTiController, the method calls the entry point. It can be seen as XuanTiController entrance.

  • XuanTiService.java
    using separate interface and implementation design principles, XuanTiService is the interface, and its implementation is XuanTiServiceImpl, XuanTiServiceImpl is the starting point.

  • XuanTiServiceImpl.java
    XuanTiServiceImpl is the entry point, which function is to inform the cut.

  • LogAspect.java
    LogAspect is cut, some notifications that will cut into the entry point in.

  • spring-common.xml
    This is the starting point and the section of the configuration file.

PressSystem project cut mechanism of Spring AOP

  1. Overall, this is LogAspect bean cut into the bean XuanTiServiceImpl

  2. In the spring-common.xml configuration file, declare a bean-XuanTiServiceImpl and LogAspect 2
    <bean class="com.tgb.service.impl.XuanTiServiceImpl" />
    <bean id="logAspect" class="com.tgb.utils.LogAspect" />

  3. In LogAspect, there are many configuration, see the following three lines. The configuration specifies
    saveLogInsert
    updateLogInsert
    deleteLogInsert
    three cut XuanTiServiceImpl notification should
    @AfterReturning(pointcut="execution(* com.tgb.service.impl.*.save(..))", argNames="returnValue", returning="returnValue")
    @AfterReturning(pointcut="execution(* com.tgb.service.impl.*.update(..))", argNames="returnValue", returning="returnValue")
    @Around("execution(* com.tgb.service.impl.*.delete(..)) && args(id, table_name)")
    be noted that each notification, marked @AspectJ embodiment different types of notifications, e.g. @AfterReturning, @Around
    addition, each notification may be configured with a pointcut, i.e. each notification where to cut

  4. AOP implementations are achieved through a proxy. So in the spring-common.xml specified in the autoproxy, create a proxy for the XuanTiServiceImpl, as follows:
    <aop:aspectj-autoproxy />
    Note: If you decide a Spring bean to be cut, then Spring will automatically generate the proxy for the bean to insert foreign methods to ensure notification be executed

  5. In the following three methods XuanTiServiceImpl in line with the three methods described pointcut therefore specifically, these three methods is the starting point, they are smoothly cut into
    public void save(XuanTi xuanTi) {...}
    public boolean update(XuanTi xuanTi) {...}
    public boolean delete(String id, String table_name) {...}

Spring AOP invocation mechanism PressSystem project

 

The following description, mainly related to 2:00, and can be summarized as @Aspect @Autowired
@Aspect is "cut mechanism", @ AfterReturning, @Around, pointcut  , <aop:aspectj-autoproxy /> and other knowledge belong to this body of knowledge
@Autowired is "automatic binding mechanism" @Component notes, broad bean and other knowledge belongs to the basic knowledge of @Autowired

In addition, call the procedure as mentioned below, is related to the process of Spring AOP minimal subset

 

  1. XuanTiController been called, in particular, which  xuanTiService.save(xuanTi); has been called

  2. Because XuanTiController have labeled @Autowired of xuanTiService, so Application will look for the bean xuanTiService

  3. As to why the bean to find xuanTiService it is because the configuration of the spring-common.xml
    <context:annotation-config />
    NOTE: Enabling notes configuration, for example, enabled the identification @Autowired.

  4. XuanTiService just an interface, realize it is XuanTiServiceImpl

  5. XuanTiServiceImpl this bean is found, because there are configured in the spring-common.xml
    <bean class="com.tgb.service.impl.XuanTiServiceImpl" />

  6. xuanTiService.save (xuanTi); was executed, specifically, XuanTiServiceImpl the public void save (XuanTi xuanTi) {...} is executed

  7. According to the description in the previous section, "cutting mechanism", in execution  public void save(XuanTi xuanTi) {...} time, accompanied by the implementation of saveLogInsert method LogAspect

  8. In LogAspect, there are labeled @Autowired of logService, so Application will look for the bean logService

  9. Find reasons, is described in point 3

  10. LogService just an interface, realize it is LogServiceImpl

  11. XuanTiServiceImpl found this bean, since there are arranged in spring-common.xml in
    <bean id="LogService" class="com.tgb.service.impl.LogServiceImpl" />
    Notes: logging business logic objects

  12. logService.log (log); was executed, specifically, LogServiceImpl the public void log (Log log) {...} is executed

 

To Learn More

In this section, I will introduce some other basics of Spring AOP Docs mentioned. These basics are not used in PressSystem project, but as the basics of Spring AOP Docs, should understand.

Implementation of the five types of notification

 

In the previous section, we mentioned five types of notification. These five types of notifications, in my aop_demo sample program has to use. Note that the method declaration notice of the following two ways. In my sample program, has demonstrated:

 

  • @Aspectj
  • Schema-based

 

Using the method of notification parameters

When a notification of the statement, is the starting point of the incoming parameters, use this parameter in the notification, for example, delete the notification referred to above:

@Around("execution(* com.tgb.service.impl.*.delete(..)) && args(id, table_name)")

When I was introduced to  (id, table_name) the parameters, you can use the id and table_name in the notification. The purpose is to delete the notification, when the delete method of execution, to write the log. I want to know in the log id and delete table_name. After passing these two parameters, you can record.
Parameters can be very complex notice to meet actual needs. For more notification parameters to use, you can view Spring AOP Docs.

 

Pointcut - the use of cut-off point of the expression

Pointcut Is a  Advice specific configuration, specified a Advice will cut into what the starting point, which is the cut-off point determined expression. Pointcut is a very important mechanism, there is a simple application in LogAspect PressSystem's. Want to know more about using, refer to Spring AOP Docs.

Introductions Introduction

 

The following is the original description Spring AOP Docs in:

Introductions (known as inter-type declarations in AspectJ) enable an aspect to declare that advised objects implement a given interface, and to provide an implementation of that interface on behalf of those objects.

Related code, and can be found in com.spring.demo08 com.spring demo14. among them:

 

  • com.spring.demo08 is achieved Schema way
  • com.spring.demo14 is the way to achieve @AspectJ

 

According to my understanding, meaning Introduction of interface type is strong turn, but when I see a comment that it was "a dynamic interface to achieve", that there is also other flower head I do not understand. . .

AOP Proxy Introduction Principles

Spring dynamic proxy in two ways: First, the JDK dynamic proxy; the other is cglib dynamic proxy (by modifying bytecode agent). JDK dynamic proxy can be a way, for example, to ascertain the superficial AOP Proxy exactly.
JDK agent mainly by way of the reflection achieved with dynamic compilation, java.lang.reflect package mainly related to two classes: Proxy and  InvocationHandler. Which  InvocationHandler is an interface, the interface definition may be achieved by transverse logic in the calling code and the target class by reflection, dynamic crosscutting and business logic braided together. In another article will focus on Spring AOP talk about the underlying implementation technologies: JDK dynamic proxy.

 

summary

Blog post was written a long time, partly because almost spare time learning, partly because there are a lot of knowledge of Spring AOP.
In the concept of aspect-oriented programming, the more important is the aspect class and target class. Method faceted aspect class, the target class has entry points. By proper configuration, the method can cut the cut target cut point class.
This blog introduces the concept of AOP, and then describes the five types of notification. Next, PressSystem for example, tells the story of PressSystem the basic components of AOP, mechanisms and processes calls cut.
Finally, the expansion of the knowledge, very briefly mentioned the implementation of the five types of notification, notification using the method parameters, Pointcut - the use of cut-point expressions, Introductions brief. The most important thing, I think the principle is AOP Proxy Introduction. About AOP Proxy Introduction Principles, see another blog: Spring AOP in JDK dynamic proxy.

Reference material

  1. Aspect Oriented Programming with the Spring 10.  (This is the Master, as knowledge of the introduction)
  2. AOP that point thing  (The article is not good, too casual, thinking is not clear)
  3. Spring AOP Example Tutorial – Aspect, Advice, Pointcut, JoinPoint, Annotations, XML Configuration (Spring AOP demo)
  4. Spring AOP complete logging  (PressSystem logging realized, reference this article)
  5. The principle of Spring AOP container - dynamic proxy  (Spring AOP proxy dynamic learning)
  6. Spring AOP underlying implementation technology -JDK dynamic proxy  (Spring AOP proxy dynamic learning)

 

Created: 2016-05-14 Saturday 6:32:24 PM

 

Guess you like

Origin www.cnblogs.com/gkmeteor/p/11799214.html