Spring深入讲解实现AOP的三种方式

 更新时间:2022年05月20日 11:17:36   作者:张志明(努力奋斗版)  
Spring的AOP就是通过动态代理实现的,使用了两个动态代理,分别是JDK的动态代理和CGLIB动态代理,本文重点给大家介绍下Spring Aop的三种实现,感兴趣的朋友一起看看吧

[重点] 使用AOP织入 需要导入一个依赖包

  <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>
    </dependencies>

方式一:使用原生Spring API接口

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    配置aop 需要导入aop的约束-->
    <aop:config>
<!--        切入点 expression表达式 expression(要执行的位置  )-->
        <aop:pointcut id="pointcut" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

Log

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
    //objects:参数
    //target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
    }
}
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    //method:要执行的目标对象的方法
    //objects:参数
    //target:目标对象
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+ method.getName() + "方法,返回结果为:"+returnValue);
    }
}

Service

import org.springframework.stereotype.Service;
@Service
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void search();
}
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
    }
    @Override
    public void delete() {
    }
    @Override
    public void update() {
    }
    @Override
    public void search() {
    }
}

test动态代理 代理的是接口(代理模式是SpringAOP的底层)

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理 代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

方式二:使用自定义类

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
    <bean id="diy" class="com.kero.diy.DiyPointCut"/>
    <aop:config>
<!--        自定义切面 ref要引用的类-->
        <aop:aspect ref="diy">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

DIY类

public class DiyPointCut {
    public void before(){
        System.out.println("````方法执行前````");
    }
    public void after(){
        System.out.println("````方法执行后````");
    }
}

其他的不变

方式三:使用注解实现

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    注册bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    开启注解支持-->
    <aop:aspectj-autoproxy/>
    <bean id="annotationPointCut" class="com.kero.diy.AnnotationPointCut"/>
</beans>

自定义类

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解方式实现AOP
@Aspect
//标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.kero.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("````方法执行前````");
    }
    @After("execution(* com.kero.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("````方法执行后````");
    }
}

其他不变

补充知识:execution表达式

execution表达式的详解

切入点表达式:execution(* 包名.*.*(..))

整个表达式可以分为五个部分:

1、execution(): 表达式主体。

2、第一个*号:方法返回类型, *号表示所有的类型。

3、包名:表示需要拦截的包名。

4、第二个*号:表示类名,*号表示所有的类。

5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面( )里面表示方法的参数,两个句点表示任何参数

其中除了返回类型模式、方法名模式和参数模式外,其它项都是可选的。

举例:

execution(public * *(..)) 匹配所有的public修饰符的方法

execution(* set*(..)) 匹配所有”set”开头的方法:

execution(* com.kero.service.UserServiceImpl.*(..))) 匹配UserServiceImpl接口/类的所有方法:

到此这篇关于Spring深入讲解实现AOP的三种方式的文章就介绍到这了,更多相关Spring AOP内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot ehcache 配置使用方法代码详解

    springboot ehcache 配置使用方法代码详解

    EhCache是一个比较成熟的Java缓存框架,Springboot对ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也简易,今天给大家分享springboot ehcache 配置使用教程,一起看看吧
    2021-06-06
  • IDEA2020.1创建springboot项目(国内脚手架)安装lombok

    IDEA2020.1创建springboot项目(国内脚手架)安装lombok

    这篇文章主要介绍了IDEA2020.1创建springboot项目(国内脚手架)安装lombok,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 详解Java中的时区类TimeZone的用法

    详解Java中的时区类TimeZone的用法

    TimeZone可以用来获取或者规定时区,也可以用来计算时差,这里我们就来详解Java中的时区类TimeZone的用法,特别要注意下面所提到的TimeZone相关的时间校准问题.
    2016-06-06
  • SpringCloud重试机制配置详解

    SpringCloud重试机制配置详解

    本篇文章主要介绍了SpringCloud重试机制配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Java编程构造方法与对象的创建详解

    Java编程构造方法与对象的创建详解

    这篇文章主要介绍了Java编程构造方法与对象的创建详解,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • eclipse的web项目实现Javaweb购物车的方法

    eclipse的web项目实现Javaweb购物车的方法

    这篇文章主要介绍了eclipse的web项目实现Javaweb购物车的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Java使用ArrayList实现扑克牌的示例代码

    Java使用ArrayList实现扑克牌的示例代码

    学习了关于集合类的知识,我们可以做一个小项目来加深对集合类知识的学习!本文就来利用ArrayList实现扑克牌发牌洗牌效果,需要的可以参考一下
    2022-10-10
  • java线程之用Thread类创建线程的方法

    java线程之用Thread类创建线程的方法

    本篇文章介绍了,Thread类创建线程的方法。需要的朋友参考下
    2013-05-05
  • Spring Cache的基本使用与实现原理详解

    Spring Cache的基本使用与实现原理详解

    缓存是实际工作中非经常常使用的一种提高性能的方法, 我们会在很多场景下来使用缓存。下面这篇文章主要给大家介绍了关于Spring Cache的基本使用与实现原理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-05-05
  • Java队列同步器之CountDownLatch实现详解

    Java队列同步器之CountDownLatch实现详解

    这篇文章主要介绍了Java队列同步器之CountDownLatch实现详解,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程执行完后再执行,例如,应用程序的主线程希望在负责启动框架服务的线程已经启动所有框架服务之后执行,需要的朋友可以参考下
    2023-12-12

最新评论