The term & Spring AOP (Aspect Oriented Programming)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42764468/article/details/102765823

0.AOP Overview

  1. The role of AOP: During the program run, without changing the source code of the existing methods enhanced
  2. Advantages:
    reduce duplication of code
    to improve development efficiency
    and easy maintenance
  3. AOP implementations: Dynamic Proxy

1. connection point (Jointpoint)

All methods are the business layer junction

2. The entry point (Pointcut)

The business layer is enhanced dynamic proxy method pointcuts

3. Notification / enhancement (Advice)

After the notice is to intercept the connection point to do
the type of notification: pre-notification, rear, throws advice, final notice, notice around the
entire invoke a method of execution is around advice
Here Insert Picture Description

4. weaving (Weaving)

The original business layer can not support transactions, now create a proxy object in the original business transaction layer was added
during the addition of transaction is called weaving

The section (Aspect)

Binding and entry point notification
entry point for the enhanced layer service category
notification provides a class of common code, such as the business transaction layer was added, the common code is

6. Spring-based xml configuration steps of aop

  1. The notification to the Spring bean to manage
  2. Use aop: config configuration label to indicate the beginning Aop
  3. Use aop: aspect tag indicates that the configuration section
     id attribute: section is to provide a unique identifier
     ref attribute: Specifies a notification class id of the bean
  4. In aop: aspect type internal label using the label corresponding to the configuration of the notification
    now wants printLog method is performed before the entry point, it is pre-notification
     aop: before represents a pre-notification
      method attribute: Specifies which method is used logger class pre-notification is
      pointcut attribute: specifies the pointcut expression,
             meaning that expression refers to a layer which is a method of enhancing the business
  5. Writing pointcut expressions:
    Keywords: execution (expression)
    expression: access modifiers return value package names package names package name ... the name of the class method name (parameter list)...
    Criteria expression written:
    public void COM .hh.service.AccountServiceImpl.saveAccount ()

Here Insert Picture Description

public interface AccountService {
    //模拟保存账户
    void saveAccount();
    //模拟更新账户
    void updateAccount(int i);
    //模拟删除账户
    int deleteAccount();
}
public class AccountServiceImpl implements AccountService {

    public void saveAccount() {
        System.out.println("执行了保存");
    }

    public void updateAccount(int i) {
        System.out.println("执行了更新");
    }

    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}
public class Logger {
    /**
     * 用于打印日志
     * 计划让其在切入点执行之前执行(切入点的方法就是业务层方法)
     */
    public void  printLog(){
        System.out.println("Logger类中的printLog方法开始记录日志");
    }
}
<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">
    <!--配置Spring的IOC-->
    <bean id="accountService" class="com.hh.service.AccountServiceImpl"/>
    <bean id="logger" class="com.hh.utils.Logger"/>
    <!--配置AOP-->
    <aop:config>
        <aop:aspect id="logAdvice" ref="logger">
            <!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
            <!--printLog为通知方法-->
            <!--pointcut代表的切入点方法-->
            <aop:before method="printLog"
                        pointcut="execution(public void com.hh.service.AccountServiceImpl.saveAccount())"/>
        </aop:aspect>
    </aop:config>
</beans>
public class AOPTest {
    public static void main(String[] args) {
        //获取容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取对象
        AccountService accountService = context.getBean("accountService", AccountService.class);
        //执行方法
        accountService.saveAccount();
    }
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42764468/article/details/102765823