spring之基于xml的AOP(面向切面)开发07

spring之基于xml的AOP(面向切面)开发07

AOP概念

简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强.

AOP的作用

在程序运行期间,不修改源码对已有方法进行增强.

优势

减少重复代码

提高开发效率

维护方便

AOP的实现方式

使用动态代理技术

AOP中相关术语

Joinpoint(连接点)

所谓连接点是指那些被拦截到的点,在spring中,这些点指的时方法,因为spring只支持方法类型的连接点

Pointcut(切入点)

所谓切入带你是指我们要对哪些连接点进行拦截的定义.

Advice(通知/增强)

所谓通知是指拦截到连接点之后要做的事情就是通知.

AOP基于xml配置的环境准备

基于maven工程导入spring的jar包(坐标)

导入解析切入表达式的jar包

pom.xml:

<packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
    </dependencies>

准备一个日志类用于打印日志,计划在切入点方法之前执行(前置通知)

 
public class Logger {
    /**
     * 用于打印日志,计划让其在切入点方法之前执行
     */
    public void pringLog(){
        System.out.println("Logger类中的printLog方法开始记录日志了...");
​
    }
​
}

创建一个bean.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: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,把service对象配置-->
    <bean id="accountService" class="cn.itcast.service.impl.AccountServiceImpl"></bean>
    <!-- spring中基于xml的AOP配置步骤
    -->
 <!--  1.把通知bean交给spring来管理 -->
    <!-- 配置Logger类-->
    <bean id="logger" class="cn.itcast.utils.Logger"></bean>
<!--配置AOP -->
   <!-- 2.使用aop:config标签表明开始AOP配置-->
    <aop:config>
      <!--
        3.使用aop:aspect标签表明配置切面
     id属性:是给切面一个唯一标识
     ref属性:是指定通知类bean的id
-->
        <aop:aspect id="logAdvice" ref="logger">
                <!--
           4.在aop:aspect标签内部使用对应的标签来通知类型
     aop:before:表示配置前置通知
     method:用于指定Logger类中的哪个方法是前置通知
     poincut属性:用于指定切入点表达式,该表达式的含义值得是也业务中的哪些方法增强
     切入点表达式:
       关键字:execution
       表达式格式:
         访问修饰符 返回值 包名.包名...类名.方法名(参数列表)
       标准的表达式写法
       public void cn.itcast.service.impl.AccountService.saveAccount()
       访问修饰符可以省略
       返回值可以使用通配符表示返回任意值
       包名可以使用通配符,表示任意包.但是有几个包,就需要写几个*
       包名可以使用..,表示当前包及其子包
       参数列表
         可以直接写数据类型
         也可以用通配符表示任意类型,但是必须有参数
         可以使用..表示有无参数即可,有参数可以是任意类型
       全通配写法:
        * *..*.*(..)
        实际开发中切入带你表达式的通常写法
         切到业务层实现类下的所有方法
          *cn.itcast.service.impl.*.*.(..)
-->
            <aop:before method="pringLog" pointcut="execution(public void cn.itcast.service.impl.AccountServiceImpl.saveAccount())" ></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

四大通知的配置

bean.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: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,把service对象配置-->
    <bean id="accountService" class="cn.itcast.service.impl.AccountServiceImpl"></bean>
    <!-- 配置Logger类-->
    <bean id="logger" class="cn.itcast.utils.Logger"></bean>
<!--配置AOP -->
    <aop:config>
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置前置通知-->
            <aop:before method="beforePringLog" pointcut="execution(public void cn.itcast.service.impl.AccountServiceImpl.saveAccount())" ></aop:before>
            <!-- 配置后置通知-->
            <aop:after-returning method="afterRuningPringLog" pointcut="execution(public void cn.itcast.service.impl.AccountServiceImpl.saveAccount())" ></aop:after-returning>
            <!-- 配置异常通知-->
            <aop:after-throwing method="afterThrowingPringLog" pointcut="execution(public void cn.itcast.service.impl.AccountServiceImpl.saveAccount())" ></aop:after-throwing>
            <!-- 配置最终通知-->
            <aop:after method="afterPringLog" pointcut="execution(public void cn.itcast.service.impl.AccountServiceImpl.saveAccount())" ></aop:after>
​
        </aop:aspect>
    </aop:config>
</beans>

环绕通知

Spring框架为我们提供了一个接口,ProceedingJoinPoint,该接口有一个方法proceed(),此方法就相当于明确调用切入方法.该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用

spring中的环绕通知

它是spring框架为我们提供的一种可以在代码中手动控制增强方法合适执行的方式.

bean.xml的配置:

<!--配置AOP -->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(public void cn.itcast.service.impl.AccountServiceImpl.saveAccount())"></aop:pointcut>
        <aop:aspect id="logAdvice" ref="logger">
      <!-- 配置环绕通知-->
            <aop:around method="arountdPrintLog" pointcut-ref="pt1"></aop:around>
        </aop:aspect>
    </aop:config>

Logger通知类的实现:

/**
     * 环绕通知
     */
    public Object arountdPrintLog(ProceedingJoinPoint pjp) {
        Object rtValue = null;
        Object[] args = pjp.getArgs();//得到方法执行所需的参数
        try {
            System.out.println("环绕通知开始记录日志了...前置");
            rtValue = pjp.proceed(args);
            System.out.println("环绕通知开始记录日志了...后置");
            return rtValue;
        } catch (Throwable throwable) {
            System.out.println("环绕通知开始记录日志了...异常");
            throwable.printStackTrace();
        } finally {
            System.out.println("环绕通知开始记录日志了...最终");
        }
        return rtValue;
​
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

全干程序员demo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值