简述SpringAOP的实现原理_spring AOP

简述SpringAOP的实现原理_spring AOPAOP用Spring需要导入包<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>方式一:使用Spring接口编写javapackage com.kuang.log;

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

AOP

用Spring需要导入包

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

在这里插入图片描述
在这里插入图片描述

方式一:使用Spring接口

  1. 编写java
package com.kuang.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice { 
   
    //method 要执行的目标对象的方法
    //args 参数
    //target 目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable { 
   
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
    }
}

package com.kuang.log;

import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice { 
   

    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { 
   
        System.out.println("执行了" + method.getName() + "返回结果为:" + returnValue);
    }
}

  1. 编写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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册bean-->
    <bean id="userService" class="com.kuang.service.UserServiceImpl"/>
    <bean id="log" class="com.kuang.log.Log"/>
    <bean id="afterLog" class="com.kuang.log.AfterLog"/>

    <!--方式一:使用原声Spring API接口-->
    <!--配置opp:需要导入opp的约束-->
    <aop:config>
        <!--切入点:expression:表达式,execution(要执行的位置!* * * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

编写测试类

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest  { 
   
    public static void main(String[] args) { 
   
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

在这里插入图片描述
方式二:使用自定义类来实现AOP

package com.kuang.diy;

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

    public void after(){ 
   
        System.out.println("===========方法执行后==========");
    }

}

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

    <!--方式二:自定义类-->
    <bean id="diy" class="com.kuang.diy.DiyPointCut"/>
    <aop:config>
        <!--自定义切面,ref要引用的类-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*())"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

测试

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest  { 
   
    public static void main(String[] args) { 
   
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

在这里插入图片描述

方式三:使用注解实现
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册bean-->
    <bean id="userService" class="com.kuang.service.UserServiceImpl"/>

    <bean id="diy" class="com.kuang.diy.DiyPointCut"/>
    <!--方式三:自定义类-->
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
</beans>
package com.kuang.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class DiyPointCut { 
   
    @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void before(){ 
   
        System.out.println("===========方法执行前==========");
    }
    @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void after(){ 
   
        System.out.println("===========方法执行后==========");
    }
    //在环绕增强中,我们可以传入一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable { 
   
        System.out.println("环绕前");

        Signature signature = jp.getSignature();//获得签名
        System.out.println("signature:" + signature);

        Object proceed = jp.proceed();
        System.out.println("环绕后");

    }

}

测试

import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest  { 
   
    public static void main(String[] args) { 
   
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168806.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 医咖会SPSS免费教程学习笔记—配对卡方检验

    医咖会SPSS免费教程学习笔记—配对卡方检验配对卡方检验(McNemar’s检验)1.需要满足的假设:(1)观测变量是二分类变量(互斥)(2)分组变量有两类(有三类及以上的用Cochran’sQ检验)2.实操分析—非参数检验—相关样本—勾选“自动比较实测数据和假设数据”—选择上方工具栏中的“字段”—将分组变量拖入“检验字段”中—选择上方工具栏中“设置”—勾选定制检验—选择“麦克尼马尔检验”—运行…

    2022年8月31日
    4
  • RAID 小结

    RAID 小结

    2021年8月18日
    55
  • Java基础入门笔记08——文件流,IO流,FileIputStream类,FileOutputStream类,缓冲流,转换流,标准输入输出流,打印流,数据流,对象流,随机存储流

    Java基础入门笔记08——文件流,IO流,FileIputStream类,FileOutputStream类,缓冲流,转换流,标准输入输出流,打印流,数据流,对象流,随机存储流IO流java.io.File类——对计算机操作系统中的文件和文件夹文件流——基于文件的操作,一般都以File开头缓冲流——基于内存的操作,一般都以BuffereDd开头转换流标准输入输出流打印流——PrintStream/PrintWriter——System.out.println数据流对象流——把一个对象转换为数据流进行读写,涉及到序列化、反序列化随机存取文件流——这里的随机是指随意,想在哪里进行就在哪里进行,进行的操作也是可以随意指定的相对路

    2022年8月8日
    18
  • python面试常见问题-Python面试中最常见的25个问题

    python面试常见问题-Python面试中最常见的25个问题Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发。Python是一个面向对象的解释型的交互式高级脚本语言。Python被设计成一种高可读性的语言,因为它大量地使用了英语中的单词作为关键字,而且不像其他语言使用标点符号构成复杂的语法结构,Python的语法结构非常少。Python是一种解释型语言:即Python程序是在运行时由解释器解释执行的,因而不用事先编…

    2022年10月19日
    3
  • 微创业平台

    一种专为大学生创业服务的平台。主旨是,让大学生创业创新,在某种比较简单,比较容易的环境里进行。让大学生创业创新,不要太多的风险,比较容易达成创业目标。云电话,一种5G新电话。特别适合大学生微创业,更需要大学生微创新。将云电话平台,建设成湖铁微创业平台,是深圳云电话平台,与湖铁职院创新创业学院,合作打造的专为湖铁职院大学生,创新创业服务的平台。建设好湖铁微创业平台,具有重大的社会意义。√成功示范作用。先在创新创业学院,打造标杆试点MVP。√便…

    2022年4月4日
    88
  • Windows Longhorn_Windows优化

    Windows Longhorn_Windows优化一、系统优化设置。1、删除Windows强加的附件:1)用记事本NOTEPAD修改/winnt/inf/sysoc.inf,用查找/替换功能,在查找框中输入,hide(一个英文逗号紧跟hide),将“替换为”框设为空。并选全部替换,这样,就把所有的,hide都去掉了,2)存盘退出,3)再运行“添加-删除程序”,就会看见“添加/删除Windows组件”中多出了好几个选项;这样你可以删

    2022年10月21日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号