Spring中的AOP术语(面向切面编程)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_42764468/article/details/102765823

0.AOP概述

  1. AOP的作用:在程序运行期间,不改变源码对已有方法进行增强
  2. 优势:
    减少重复代码
    提高开发效率
    维护方便
  3. AOP的实现方式:动态代理

1.连接点(Jointpoint)

业务层中的所有方法都是连接点

2.切入点(Pointcut)

业务层中被动态代理增强的方法切入点

3.通知/增强(Advice)

通知就是拦截到连接点之后要做的事情
通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知
整个invoke方法的执行为环绕通知
在这里插入图片描述

4.织入(Weaving)

原来的业务层无法支持事务,现在创建代理对象在原有的业务层加入了事务
加入事务的过程就叫织入

5.切面(Aspect)

切入点和通知的结合
切入点是业务层被增强的类
通知是提供了公共代码的类,比如在业务层加入的事务,就是公共代码

6. Spring中基于xml的aop配置步骤

  1. 把通知bean交给Spring来管理
  2. 使用aop:config 标签表明开始Aop的配置
  3. 使用aop:aspect 标签表明配置切面
     id属性:是给切面提供一个唯一标识
     ref属性:指定通知类bean的id
  4. 在aop:aspect 标签内部使用对应的标签配置通知的类型
    现在想让printLog方法在切入点方法之前执行,所以是前置通知
     aop:before代表前置通知
      method属性:用于指定logger类中的哪个方法是前置通知
      pointcut属性:用于指定切入点表达式,
             该表达式的含义指的是对业务层中哪些方法增强
  5. 切入点表达式的写法:
    关键字:execution(表达式)
    表达式:访问修饰符 返回值 包名.包名.包名…类名.方法名(参数列表)
    标准的表达式写法:
    public void com.hh.service.AccountServiceImpl.saveAccount()

在这里插入图片描述

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();
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42764468/article/details/102765823
今日推荐