SpringAop总结

简介: SpringAop总结

1.我对Aop的理解是在不改变源代码的基础上,扩展新的功能。

2.概念解释:

public class User {

/**
 * 连接点:类里面可以被增强的方法
 * 切入点:类里面已经被增强的方法
 * 通知/增强:扩展的功能(逻辑)
 *    ①前置通知:在方法之前执行
 *    ②后置通知:在方法之后执行(出现异常也会执行)
 *    ③异常通知:方法出现异常时候执行
 *    ④返回通知:在后置之前执行(出现异常不会执行,方法成功返回后执行)
 *    ⑤环绕通知:在方法之前和之后执行(包住方法)
 *  切面:把增强用到方法的过程叫增强(注意是一个过程)
 */
public void show(){//这个show方法叫 连接点

    System.out.print("我是AopUser");
}

}
3.spring使用aspectj来做aop

①aspectj不是spring的一部分

②spring2.0新增了对aspectj的支持

4.实现aop有2种方式

①xml方式

②注解方式

5.导入jar包

在导入spring核心包的基础上,还需要导入aop相关的jar包

spring-aspects-5.0.5.RELEASE.jar

spring-aop-5.0.5.RELEASE.jar

aopalliance.jar 

aspectjweaver-1.8.7.jar

6.常用表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

(1) execution(* test3.AopUser.show1(..))

(2) execution( test3.AopUser.(..))

(3) execution( .*(..))

(4)execution( show(..))匹配所有show开头的方法

7.xml方式

①创建User类

@Component
public class User {

public void show(){
    //int i=1/0;异常通知的时候用
    System.out.println("我是User的show方法");
}

}

②创建增强类:

public class AopMethod {

public void before(){
    System.out.println("我是前置通知");
}
public void after(){
    System.out.println("我是后置通知");
}
public void last(){
    System.out.println("我是最终通知");
}
public void exception(){
    System.out.println("我是异常通知");
}
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
    System.out.println("环绕通知开始");
    proceedingJoinPoint.proceed();
    System.out.println("环绕通知结束");
}

}
③applicationContext.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
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="test3"></context:component-scan>

<bean id="AopMethod" class="test3.AopMethod"></bean>

<aop:config>
    <aop:pointcut id="userShow" expression="execution(* test3.User.show(..))"></aop:pointcut>
    <aop:aspect ref="AopMethod">
        <!--<aop:before method="before" pointcut-ref="userShow"></aop:before>-->
        <!--<aop:after method="after" pointcut-ref="userShow"></aop:after>-->
        <!--<aop:around method="around" pointcut-ref="userShow"></aop:around>-->
       <!-- <aop:after-throwing method="exception" pointcut-ref="userShow"></aop:after-throwing>-->
        <!--<aop:after-returning method="last" pointcut-ref="userShow"></aop:after-returning>-->
    </aop:aspect>
</aop:config>


④测试类MainTest

public class MainTest {

public static void main(String[] args){
    //1.加载xml文件
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.拿到user对象,这里user是bean里面的id
    User user = (User)context.getBean("user");
    user.show();

}

}
⑤运行结果...

8.注解方式

@Before前置通知

@AfterReturning返回通知

@Around环绕通知

@AfterThrowing异常通知

@After(后置通知)最终通知(最终final通知,不管是否异常,该通知都会执行)

①在applicationContext.xml中加入

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


applicationContext.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
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="test3"></context:component-scan>

<!--开启aop操作-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

②User类:

@Component
public class User {

public void show(){
    //int i=1/0;//异常通知的时候用
    System.out.println("我是User的show方法");
}

}
③AopMethod类:

@Component
@Aspect
public class AopMethod {

@Before(value = "execution(* test3.User.show(..))")
public void before(){
    System.out.println("我是前置通知");
}
@AfterReturning(value = "execution(* test3.User.show(..))")
public void after(){
    System.out.println("我是返回通知");
}
@After(value = "execution(* test3.User.show(..))")
public void last(){
    System.out.println("我是后置通知");
}
@AfterThrowing(value = "execution(* test3.User.show(..))")
public void exception(){
    System.out.println("我是异常通知");
}
@Around(value = "execution(* test3.User.show(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
    System.out.println("环绕通知开始");
    proceedingJoinPoint.proceed();
    System.out.println("环绕通知结束");
}

}
④MainTest类:

public class MainTest {

public static void main(String[] args){
    //1.加载xml文件
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.拿到user对象,这里user是bean里面的id
    User user = (User)context.getBean("user");
    user.show();

}

}
⑤运行结果:
image.png

相关文章
|
1月前
|
Java
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
这篇文章是Spring5框架的实战教程,深入讲解了AOP的基本概念、如何利用动态代理实现AOP,特别是通过JDK动态代理机制在不修改源代码的情况下为业务逻辑添加新功能,降低代码耦合度,并通过具体代码示例演示了JDK动态代理的实现过程。
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
|
2月前
|
Java Spring
在Spring Boot中使用AOP实现日志切面
在Spring Boot中使用AOP实现日志切面
|
7天前
|
Java 数据库连接 数据库
Spring基础3——AOP,事务管理
AOP简介、入门案例、工作流程、切入点表达式、环绕通知、通知获取参数或返回值或异常、事务管理
Spring基础3——AOP,事务管理
|
1月前
|
XML Java 数据格式
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
这篇文章是Spring5框架的AOP切面编程教程,通过XML配置方式,详细讲解了如何创建被增强类和增强类,如何在Spring配置文件中定义切入点和切面,以及如何将增强逻辑应用到具体方法上。文章通过具体的代码示例和测试结果,展示了使用XML配置实现AOP的过程,并强调了虽然注解开发更为便捷,但掌握XML配置也是非常重要的。
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
|
20天前
|
缓存 Java 开发者
Spring高手之路22——AOP切面类的封装与解析
本篇文章深入解析了Spring AOP的工作机制,包括Advisor和TargetSource的构建与作用。通过详尽的源码分析和实际案例,帮助开发者全面理解AOP的核心技术,提升在实际项目中的应用能力。
18 0
Spring高手之路22——AOP切面类的封装与解析
|
1月前
|
安全 Java 开发者
Java 新手入门:Spring 两大利器IoC 和 AOP,小白也能轻松理解!
Java 新手入门:Spring 两大利器IoC 和 AOP,小白也能轻松理解!
29 1
|
1月前
|
Java Spring
Spring的AOP组件详解
该文章主要介绍了Spring AOP(面向切面编程)组件的实现原理,包括Spring AOP的基础概念、动态代理模式、AOP组件的实现以及Spring选择JDK动态代理或CGLIB动态代理的依据。
Spring的AOP组件详解
|
1月前
|
Java API Spring
Spring Boot 中的 AOP 处理
对 Spring Boot 中的切面 AOP 做了详细的讲解,主要介绍了 Spring Boot 中 AOP 的引入,常用注解的使用,参数的使用,以及常用 api 的介绍。AOP 在实际项目中很有用,对切面方法执行前后都可以根据具体的业务,做相应的预处理或者增强处理,同时也可以用作异常捕获处理,可以根据具体业务场景,合理去使用 AOP。
|
20天前
|
Java Spring XML
掌握面向切面编程的秘密武器:Spring AOP 让你的代码优雅转身,横切关注点再也不是难题!
【8月更文挑战第31天】面向切面编程(AOP)通过切面封装横切关注点,如日志记录、事务管理等,使业务逻辑更清晰。Spring AOP提供强大工具,无需在业务代码中硬编码这些功能。本文将深入探讨Spring AOP的概念、工作原理及实际应用,展示如何通过基于注解的配置创建切面,优化代码结构并提高可维护性。通过示例说明如何定义切面类、通知方法及其应用时机,实现方法调用前后的日志记录,展示AOP在分离关注点和添加新功能方面的优势。
30 0
|
29天前
|
缓存 安全 Java
Spring AOP 中两种代理类型的限制
【8月更文挑战第22天】
14 0