spring framework-AOP

1. AOP concept of spring framework

A key component of the Spring framework is the aspect-oriented programming (AOP) framework. Aspect-oriented programming needs to decompose the program logic into different parts called the so-called concerns. Functions that span multiple points of an application are called cross-cutting concerns, and these cross-cutting concerns are conceptually independent of the business logic of the application.

This concept may be very abstract, but you only need to know that AOP is equivalent to an interceptor used to intercept methods or programs. You can add additional functions before or after the method is executed.

Two, AOP related terms


Insert picture description hereTypes of related term notifications (cut-in types)

  1. Pre-notification <aop:before></aop:before>: Perform notification before a method is executed.
  2. Post notification <aop:after></aop:after>: After a method is executed, the notification is executed regardless of the result.
  3. Notification after return <aop:after-returning></aop:after-returning>: After a method is executed, the notification can only be executed when the method completes successfully.
  4. Notification after an exception is thrown <aop:after-throwing></aop:after-throwing>: After a method is executed, the notification can only be executed when the method exits and throws an exception
  5. Around notification <aop:around></aop:round>: Perform notification before and after the suggestion method is called.

3. Examples (deposit and deposit business)

1. Related category catalogue
Insert picture description here
2. BankDao (Check and EmpDao are optional)

package sc.dao;

public interface BankDao {
    
    
    //转账
    public void remirt();
    //存钱
    public void save();
}

3 、 AdminCheck

package sc.impl;

import sc.dao.Check;

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

4. BankDaoImpl (EmpDaoImpl not required)

package sc.impl;

import sc.dao.BankDao;

public class BankDaoImpl implements BankDao{
    
    
    @Override
    public void remirt() {
    
    
        System.out.println("转账的业务");
    }

    @Override
    public void save() {
    
    
        System.out.println("存钱的业务");
    }
}

5、logManager

package sc.impl;

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

6、TransactionManager

package sc.impl;

public class TransactionManager {
    
    
    public void begin(){
    
    
        System.out.println("开始业务逻辑");
    }
    public void commit(){
    
    
        System.out.println("提交业务逻辑");
    }

}

7、spring.xml

<?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
">
    <bean id="adminCheck" class="sc.impl.AdminCheck"></bean>
    <bean id="transactionManager" class="sc.impl.TransactionManager"></bean>
    <bean id="logManager" class="sc.impl.LogManager"></bean>
    <bean id="bankDao" class="sc.impl.BankDaoImpl"></bean>
    <bean id="empDao" class="sc.impl.EmpDaoImpl"></bean>

    <!--开始spring aop 面向切面-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="service" expression="execution(* sc.impl.*.*(..))"></aop:pointcut>

        <aop:aspect ref="adminCheck">
            <!--在此之前切入-->
            <aop:before method="check" pointcut-ref="service"></aop:before>
        </aop:aspect>

        <aop:aspect ref="transactionManager">
            <aop:before method="begin" pointcut-ref="service"></aop:before>
        </aop:aspect>


        <!--after method 用于那个方法是最终通知  谁在最前面  最后输出就在最后 -->
        <aop:aspect ref="logManager">
            <aop:after method="writelog" pointcut-ref="service"></aop:after>
        </aop:aspect>

        <aop:aspect ref="transactionManager">
            <aop:after method="commit" pointcut-ref="service"></aop:after>
        </aop:aspect>


    </aop:config>

</beans>

8、test

package sc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sc.dao.BankDao;
import sc.dao.Check;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        BankDao bankDao = (BankDao) ac.getBean("bankDao");
        bankDao.remirt();
        System.out.println("----------------------");
        bankDao.save();
    }
}

The result of the operation is
Insert picture description herethat this can reduce the redundancy of the code. You will find that the verification start, the start of the business logic, the submission of the business logic, and the log are being written only once, but it can be used twice.

Guess you like

Origin blog.csdn.net/s001125/article/details/114596163