Spring AOP understand

Refer to the link

Introduction of AOP

AOP (aspect oriented programming) can be said to be a supplement to OOP. When using OOP, when we write code in daily life, once involving a larger project, the project is indispensable for transaction processing, security processing, verification processing… And so on a large number of unrelated to our business core logic but need code here call it – the symbiotic, this time using pure OOP has started a bit overwhelmed, what’s more, our project to upgrade and expand in the future, a substantial extension on source code is not reality, so this is the reason why we study AOP.

AOP divides software into two categories: core concerns (main business code) and crosscutting concerns (symbiotic code). Separating and organizing code is AOP’s job!

We all know that OOP programming is longitudinal, while the AOP based on the transverse business logic code, and the symbiotic code woven into the tangent point, there is no trace of the original structure recovery program, this is the process of organization of the AOP on two types of code, and no modifications to native code, it has realized the function of the extension, AOP is no invasion of the native code. This gives us a bit of an understanding of AOP’s capabilities, and the symbiotic code is covered above, so we’ll get a bit of an introduction to AOP terminology.

AOP terminology

  • Advice: Enhanced processing in an AOP framework. The advice describes when and how the enhanced processing is performed by the aspect.
  • Join point: A join point is a point at which a section can be inserted during application execution. This point can be a method call or an exception thrown. In Spring AOP, join points are always method calls.
  • Pointcuts: Join points where enhanced processing can be inserted.
  • Aspect: An Aspect is a combination of advice and pointcuts.
  • Introduction: Introduction allows you to add new methods or attributes to an existing class.
  • Weaving: Weaving is the process of adding enhancement processing to the target object and creating an enhanced object.

Concepts always seem a little confusing, and the above terms are translated differently from reference book to reference book, so you need to understand them slowly in the application.

AOP implementations

Native code (core focus) TestB:

public class TestB implements C{
    @Override
    public void add(a) {
        System.out.println("I'm native to TestB!"); }}Copy the code

Symbiotic code (crosscutting concerns) MyAspect:

public class MyAspect {
    // Pre-notification
    public void before(a){
        System.out.println("I am Before!!!!");
    }
    // post notification
    public void after(a){
        System.out.println("I am After!!!!"); }}Copy the code

Spring configuration file:


      
<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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <! -- Configure object A -->
    <bean id="a" class="com.lyl.pojo.TestB"/>
    <! -- Configure the Aspect section class, which contains notifications -->
    <bean id="aspect" class="com.lyl.schemaAop.MyAspect"/>

    <! Configure SpringAop assembly rules -->
    <aop:config>
        <! TestB add(); TestB add();
        <aop:pointcut id="point" expression="execution(* com.lyl.pojo.TestB.add(..) )"/>
        <! -- Section class assembly -->
        <aop:aspect id="at" ref="aspect">
        <! -- method: method of notification; Pointcut-ref: target pointcut for organization -->
            <! -- Pre-notification -->
            <aop:before method="before" pointcut-ref="point"/>
            <! -- Post-notification -->
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>
Copy the code

Results:

I'm Before!!!! I'm native to TestB! I'm After!!!!Copy the code

AOP summary

As you can see from the simple implementation of SpringAOP above, our AOP features:

  1. AOP extensions do not invade native code
  2. OOP vertically, AOP horizontally, native code that needs to be enhanced is called pointcut, and the enhancement process is called weaving
  3. While we programmers focus primarily on core concerns, AOP focuses on crosscutting concerns and weaves them into our core concerns.

We need to pay attention to:

  1. AOP is not a framework, but a design pattern
  2. The ultimate role of AOP is to loosen coupling, solve code reuse, facilitate function extension, reduce program complexity and coupling degree, and improve development efficiency.
  3. In AOP, we refer to this behavior, which has common logic and is entangled with the core logic of other modules, as a “Crosscutting Concern” because it crosses the typical responsibility boundaries in a given programming model (the vertical model).

With future study and practice, you will have a deeper understanding of AOP.