AOP aspect-oriented programming in Spring

AOP aspect-oriented programming in Spring

One, what is AOP
AOP (Aspect Oriented Programming) is the content of the Spring framework. It is aimed at a certain step or stage of an event. It is mainly used in: logging, transaction processing, exception handling, etc., it The principle is: through filtering, directly add the set method to some specified operations, without frequent calls, and realize the dynamic proxy of java without using the interface.
AOP technology uses a technique called "cross-cutting" to dissect the inside of the encapsulated object, encapsulate the common behaviors affecting multiple classes into a reusable module, and name it Aspect. The so-called aspect, in simple terms, is the logic that has nothing to do with the business, but is called by the business modules. It can be encapsulated to reduce the repetitive code of the system, reduce the coupling degree of the modules, and take advantage of future operability and maintainability. .
The use of AOP can isolate various parts of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the development efficiency at the same time.
The Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add additional functions before or after the method is executed.
Two AOP terminology
Insert picture description here
Three notification types
Spring can use the following five types of notifications:
Insert picture description here
Four Spring's AOP-based XML architecture.
In terms of the deposit and transfer processes of the banking system, the same verification process exists. The AOP idea is Separate these same processes, use deposit/transfer methods as the entry point, and establish configurations before or after this entry point.
Code presentation:
In order to use the aop namespace tag in the description of this section, you need to import the spring-aop j architecture, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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
</beans>                         ">

First create the deposit and transfer interface

package cn.tb.dao;

public interface BankDao {
    
    
    //存钱
    public void saveMoney();

    //转账
    public void remirt();
}

Then set up the relevant process to
verify the transaction

package cn.tb.impl;

public class AdminCheck {
    
    
    public void check(){
    
    
        System.out.println("权限验证开始");
    }
}

Processing and committing the transaction

package cn.tb.impl;

import org.aspectj.lang.ProceedingJoinPoint;

public class TransactionManager {
    
    

    public void beginTransaction(){
    
    
        System.out.println("开始事务处理");
    }

    public void commitTrnasaction(){
    
    
        System.out.println("提交事务处理");
    }

    public void doInceptor(ProceedingJoinPoint point) throws Throwable{
    
    
        beginTransaction();
        point.proceed();
        commitTrnasaction();
    }
}

Perform related operations

package cn.tb.impl;

import cn.tb.dao.BankDao;

public class BankDaoImpl implements BankDao{
    
    
    @Override
    public void saveMoney() {
    
    
        System.out.println("正在存钱");
    }

    @Override
    public void remirt() {
    
    
        System.out.println("正在转账");
    }
}

Log write

package cn.tb.impl;

public class LogManager {
    
    
    //日志
    public void writeLog(){
    
    
        System.out.println("日志正在写入");
    }
}

Finally, configure the complete AOP-based XML file in Spring

<?xml version="1.0" encoding="UTF-8"?>
<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
                           ">

    <!--注入bean-->
    <bean class="cn.tb.impl.AdminCheck" id="adminCheck"></bean>
    <bean class="cn.tb.impl.BankDaoImpl" id="bankDao"></bean>
    <bean class="cn.tb.impl.LogManager" id="logManager"></bean>
    <bean class="cn.tb.impl.TransactionManager" id="transactionManager"></bean>

    <!--开始spring aop 面向切面配置-->
    <aop:config>
        
        <!--切入点 什么时机,什么时候切入-->
        <aop:pointcut id="serviceInceptor" expression="execution(* cn.tb.impl.*.*(..))"></aop:pointcut>

        <!--前置通知 在一个方法执行之前,执行通知。-->
        <aop:aspect ref="adminCheck">
            <aop:before method="check" pointcut-ref="serviceInceptor"></aop:before>
        </aop:aspect>

        <!--环绕通知 在建议方法调用之前和之后,执行通知。-->
        <aop:aspect ref="transactionManager">
            <aop:around method="doInceptor" pointcut-ref="serviceInceptor"></aop:around>
        </aop:aspect>

        <!--后置通知 在一个方法执行之后,不考虑其结果,执行通知。-->
        <aop:aspect ref="logManager">
            <aop:after method="writeLog" pointcut-ref="serviceInceptor"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

test

package cn.tb.test;

import cn.tb.dao.BankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        test1();
        System.out.println("-------------");
        test2();
    }
    //存钱
    private static void test1() {
    
    
        ApplicationContext alc = new ClassPathXmlApplicationContext("Spring.xml");
        BankDao dao = alc.getBean("bankDao", BankDao.class);
        dao.saveMoney();
    }
    //转账
    private static void test2() {
    
    
        ApplicationContext alc = new ClassPathXmlApplicationContext("Spring.xml");
        BankDao dao = alc.getBean("bankDao", BankDao.class);
        dao.remirt();
    }
}

Test Results:
Insert picture description here

Guess you like

Origin blog.csdn.net/tan1024/article/details/114938762