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

0.AOP概述

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

1.连接点(Jointpoint)

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

2.切入点(Pointcut)

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

3.通知/增强(Advice)

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

4.织入(Weaving)

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

5.切面(Aspect)

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

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

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方法开始记录日志");
    }
}
  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()

在这里插入图片描述

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我一直在流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值