SpringAOP的思想和实现方式

对于 AOP 这种编程思想,很多框架都进行了实现。Spring 就是其中之一,可以完成面向切面编程。
AspectJ 也实现了 AOP 的功能,且其实现方式更为简捷,使用更为方便,而且还支持注解式开发。所以,Spring 又将 AspectJ 的对于 AOP 的实现也引入到了自己的框架中。
我们开发的时候一般都会使用AspectJ的实现方式。

通知类型

AspectJ 中常用的通知有五种类型:
(1)前置通知
(2)后置通知
(3)环绕通知
(4)异常通知
(5)最终通知

切入点表达式

AspectJ 定义了专门的表达式用于指定切入点。表达式的原型是:
execution(modifiers-pattern? ret-type-pattern 
declaring-type-pattern?name-pattern(param-pattern)
 throws-pattern?)
 说明:
 execution(访问权限 方法返回值 方法声明(参数) 异常类型)
 访问权限和异常类型是可以省略的。

下面说AspectJ的使用需要的jar包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.5.RELEASE</version>
    </dependency>
jar包版本不对的话可能会有有类找不到异常

spring中使用aop
1:创建目标接口和实现类
2:定义切面类
在切面类中指定切入点、指定切入的时机和切入的内容

package com.dongmu.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyAspectJ {
    @Before(value = "execution(* *(..))")
    public void before(){
        System.out.println("在目标方法执行之前执行的方法");
    }

    @After(value = "execution(* *(..))")
    public void after(){
        System.out.println("在目标方法执行之后执行的方法。");
    }
}

3:写配置文件
将切面类和目标类注册到bean中。

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="goal" class="com.dongmu.target.Goal"/>

    <bean id="MyAspectJ" class="com.dongmu.aop.MyAspectJ"/>
    <context:component-scan base-package="com.dongmu.aop"/>

    <aop:aspectj-autoproxy/>
</beans>

下面还是再介绍一下SpringAOP的实现方法吧
1:加入依赖

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

2:写增强的方法

package com.dongmu.aop;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class after implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("目标方法执行之后的方法!");
    }
}

package com.dongmu.aop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class before implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("目标方法执行之前的方法!");
    }
}

3:写目标接口和实现类

package com.dongmu.goal;

public interface inter {
    public void goal1();
}

package com.dongmu.goal;

public class sp1 implements inter{
    public void goal1(){
        System.out.println("目标方法执行了!");
    }
}

4:配置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-3.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="goal1" class="com.dongmu.goal.sp1"/>
    <bean id="after" class="com.dongmu.aop.after"/>
    <bean id="before" class="com.dongmu.aop.before"/>

    <aop:config>
        <aop:pointcut id="point1" expression="execution(* com.dongmu.goal.*.*(..) )"/>
        <aop:advisor advice-ref="after" pointcut-ref="point1"/>
        <aop:advisor advice-ref="before" pointcut-ref="point1"/>
    </aop:config>
</beans>

5:测试
在这里插入图片描述

第二种方式其实还有一种方式就是自定义切入的类,就是不继承其他的类。
这个时候这么写:div是我们自定义的类,before是里面的方法

<bean id="divbefore" class="com.dongmu.aop.div"/>
<aop:config >
        <aop:aspect ref="divbefore">
            <aop:pointcut id="point2" expression="execution(* com.dongmu.goal.*.*(..) )"/>
            <aop:before method="before" pointcut-ref="point2"/>
        </aop:aspect>
    </aop:config>

在这里插入图片描述

其实步骤基本上都是一样的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北海冥鱼未眠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值