AOP(面向切面编程) 及Mybatis简介

文章目录

AOP(面向切面编程)

AOP和OOP的比较

OOP语言提供了类与类之间纵向的关系(继承、接口),而AOP补充了横向的关系(比如在不改变目标类中源代码的情况下给com.john.demo.dao包下所有类中以insert和update开头的方法添加事务管理)

AspectJ(切面)

我们将自己需要插入到目标业务逻辑中的代码模块化, 通过AOP使之可以横切多个类的模块,称之为切面。

在Spring AOP配置中切面通常包含三部分:

  • 切面模块本身
  • 通知
  • 切入点

示例:

<!-- 在beans标签中配置命名空间aop -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

   
<!-- 目标业务逻辑代码 -->
<bean id="calc" class="com.lanou3g.spring.simple.calc.CalcImpl"/>

<!-- 切面模块化对象(代表我们要附加到原始业务逻辑中的代码) -->
<bean id="calcAspect" class="com.lanou3g.spring.simple.calc.CalcAspect" />

<!-- 示例说明: 将切面calcAspect中的代码插入到calc原始业务代码中 -->
<aop:config>
    <!-- 定义公用的切入点表达式,如果aspect中有多个通知,都可以通过pointcut-ref复用 -->
    <aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.simple.calc.CalcImpl.*(..))" />
    <aop:aspect ref="calcAspect">
        <!-- 切面包含的通知(什么时间)、切入点(什么地点) -->
        <aop:around method="computeTime" pointcut-ref="all_calc_method" />
    </aop:aspect>
</aop:config>

</beans>

在pom.xml文件中添加依赖

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

切入点 (Pointcut)

在 Spring AOP 中,需要使用 AspectJ 的切点表达式来定义切点。

AspectJ 指示器 描述
execution () 用于匹配连接点的执行方法 最常用
args () 限制连接点的指定参数为指定类型的执行方法
@args () 限制连接点匹配参数类型由指定注解标注的执行方法
this () 限制连接点匹配 AOP 代理的 Bean 引用为指定类型的类
target () 限制连接点匹配特定的执行对象,目标对象是指定的类型
@target () 限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型注解
within() 限制连接点匹配指定类型,比如哪个包下,或哪个类里面
@within() 限制连接点匹配指定注释所标注的类型(当使用 Spring AOP 时,方法定义在由指定的注解所标注的类里)
@annotation 限制匹配带有指定注释的连接点

通知(Advice)

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <bean id="calc" class="com.lanou3g.spring.simple.calc.CalcImpl"/>

    <bean id="calcAspect" class="com.lanou3g.spring.simple.calc.CalcAspect" />

    <aop:config>
        <aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.simple.calc.CalcImpl.*(..))" />
        <aop:aspect ref="calcAspect">
            <aop:around method="aroundM" pointcut-ref="all_calc_method" />
            <aop:before method="beforeM" pointcut-ref="all_calc_method" />
            <aop:after-returning method="afterReturningM" pointcut-ref="all_calc_method" returning="retVal" />
            <aop:after-throwing method="afterThrowing" pointcut-ref="all_calc_method" throwing="throwable" />
            <aop:after method="afterFinallyM" pointcut-ref="all_calc_method" />
        </aop:aspect>
    </aop:config>

</beans>

配置logback.xml

<configuration>

    <property name="HOME_LOG" value="logs"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>

        <!-- 测试部署时使用如下配置 -->
        <!-- 可让每天产生一个日志文件,最多 30 个,更早的删除 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${HOME_LOG}/log-%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>10</maxHistory>
        </rollingPolicy>

        <!--
             RollingFileAppender 一般情况下需要配置两个参数:
             RollingPolicy,负责滚动。TriggeringPolicy,决定是否以及何时进行滚动
             TimeBasedRollingPolicy比较特殊,它同时继承了RollingPolicy和TriggerPolicy。

        -->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
                %msg%n
            </pattern>
        </encoder>

        <!-- 正式部署时使用此配置 -->
        <!--
            <file>${app.home}/logs/log.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>${app.home}/logs/log.%i.log.zip
                </fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>7</maxIndex>
            </rollingPolicy>

            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
                    %msg%n
                </pattern>
            </encoder>

            <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>50MB</maxFileSize>
            </triggeringPolicy>
         -->
    </appender>

    <logger name="com.lanou3g.spring" level="DEBUG"/>
    <logger name="org.springframework" level="ERROR"/>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="RollingFile" />
    </root>
</configuration>

环绕通知(around)

  • 在目标方法执行前、后被通知, 可以获取连接点对象(ProceedingJoinPoint, 该对象可以获取被拦截方法的签名、参数、返回值、包括调用与否)
  • 该方法的返回值,即代表了真正业务逻辑代码的返回值
  • 可以选择终止或正常执行目标方法

前置通知(before)

在目标方法调用前通知切面, 什么参数也无法获取。也不能终止目标方法执行

后置(返回值)通知(after returning)

只有在目标方法 正常 执行结束后才会通知, 在通知方法中可以获取到方法的返回值

后置(最终)通知 (after)

在目标方法执行结束后通知切面, 什么参数也无法获取。无论目标方法是正常执行结束还是抛出异常终止,都会被通知

异常通知(after throwing)

只有在目标方法 出现异常 才会通知, 在通知方法中可以获取到抛出的异常信息

连接点(JoinPoint)

连接点有很多种,比如方法执行期间(开始执行、执行结束、抛出异常)、字段修饰符、字段值被更改…

在Spring AOP中只支持方法连接点(因为Spring AOP底层是通过动态代理实现的)。

连接点与切入点的关系可以简单理解为: 切入点一定是连接点, 连接点不一定是切入点。

织入(Weaver)

织入的过程其实就是Spring AOP帮我们把切面中的代码织入到目标代码中的过程。

用XML方式启用Spring AOP

添加依赖

首先我们需要先将aspectJ的依赖导入maven项目中

<dependency>           
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.4</version>
</dependency>

使用示例

这里给出一个需要被织入的Java类的示例

public class CalcImpl implements Calc {
   
    /**
    * 加法
    */
    @Override
    public int add(int num1, int num2) {
   
        return num1 + num2;
    }
	/**
    * 减法
    */
    @Override
    public int subtract(int minuend, int subtrahend) {
   
        return minuend - subtrahend;
    }
    /**
    * 乘法
    */
    @Override
    public int multiply(int num1, int num2) {
   
        return num1 * num2;
    }
    /**
    * 除法
    */
    @Override
    public int divide(int dividend, int divisor) {
   
        return dividend / divisor;
    }
}

以及切面类

@Slf4j//该注解用于下面使用log输出(不想使用log的换成控制台输出语句即可)
public class CalcAspect {
   

    public Object aroundM(ProceedingJoinPoint joinPoint) throws Throwable {
   
        // 获取连接点代表的方法的签名
        Signature signature = joinPoint.getSignature();
        // 获取目标方法名
        String methodName = signature.getName();
        // 获取目标方法的参数
        Object[] args = joinPoint.getArgs();
        log.debug("[aroundM] ---- 目标方法[" + methodName + "(" + Arrays.toString(args) + ")]开始执行");
        long start = System.currentTimeMillis(
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值