spring aop的简单实现例子

xiaoxiao2021-02-28  32

转载请声明出处

1 什么是AOP

    Spring 是解决实际开发中的一些问题:     AOP 解决OOP 中遇到的一些问题.是OOP 的延续和扩展.

2 为什么学习AOP

对程序进行增强:不修改源码的情况下. AOP 可以进行权限校验,日志记录,性能监控,事务控制.

3 Spring 的AOP 的由来:

AOP 最早由AOP 联盟的组织提出的,制定了一套规范.Spring 将AOP 思想引入到框架中,必须遵守AOP 联盟 的规范.

4 底层实现:

代理机制:

Spring 的AOP 的底层用到两种代理机制:     a.  JDK 的动态代理 : 针对实现了接口的类产生代理.

    b. Cglib 的动态代理 :针对没有实现接口的类产生代理. 应用

5. AOP 的开发中的相关术语:

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring 中,这些点指的是方法,因为spring 只支持方法类型的连接点.Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint 进行拦截的定义.Advice(通知/增强):所谓通知是指拦截到Joinpoint 之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或Field.Target(目标对象):代理的目标对象

Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程.

spring 采用动态代理织入,而AspectJ 采用编译期织入和类装在期织入Proxy(代理):一个类被AOP 织入增强后,就产生一个结果代理类Aspect(切面): 是切入点和通知(引介)的结合

spring 采用动态代理织入,而AspectJ 采用编译期织入和类装在期织入Proxy(代理):一个类被AOP 织入增强后,就产生一个结果代理类Aspect(切面): 是切入点和通知(引介)的结合

6 下面直接上代码例子并附上注解

需要拦截的类
package cn.cai.service.impl; import org.springframework.stereotype.Service; import cn.cai.service.UserService; @Service("userService") public class UserServiceImpl implements UserService { @Override public void save() { System.err.println("保存用户信息"); } }

6.1 给予xml文件的配置方式

准备通知类
package cn.cai.aspect; import org.aspectj.lang.ProceedingJoinPoint; /** * aop通知类 * @author 蔡gh_baby * */ public class MyAdvice {         /*         * 前置通知==>目标方法调用前调用 * 后置通知(如果出现异常不会调用)==>目标方法调用后调用 * 环绕通知==>(目标方法调用之前和之后都调用 * 异常拦截通知==>出现异常就调用 * 后置通知(无论是否出现异常都会调用)==>目标方法调用后调用         */ public void before(){System.out.println("这是前置通知");} //后置通知(如果出现异常不会调用)public void afterReturnning(){System.out.println("这是后置通知(如果出现异常不会调用)");} //环绕通知public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{System.out.println("这是环绕通知之前的部分");//调用目标方法Object proceed = proceedingJoinPoint.proceed();System.out.println("这是环绕通知之后的部分");return proceed;} //异常拦截通知public void afterException(){System.out.println("这是异常拦截通知");} //后置通知(无论是否出现异常都会调用)public void after(){System.out.println("后置通知(无论是否出现异常都会调用)");}}
在xml文件中配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 准备工作: 导入aop(约束)命名空间 --> <!-- 1.配置目标对象 --> <bean name="userService" class="cn.cai.service.UserService"></bean> <!-- 2.配置通知对象 --> <bean name="myAdvice" class="cn.cai.aspect.MyAdvice" ></bean> <!-- 3.配置将通知织入目标对象 --> <aop:config> <!-- 配置切入点 演化: public void cn.itcast.service.UserServiceImpl.save() void cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.*() * cn.itcast.service.*ServiceImpl.*(..) * cn.itcast.service..*ServiceImpl.*(..)          --> <aop:pointcut expression="execution(* cn.cai.service.*ServiceImpl.*(..))" id="pc"/> <aop:aspect ref="myAdvice" > <!-- 指定名为before方法作为前置通知 --> <aop:before method="before" pointcut-ref="pc" /> <!-- 后置 --> <aop:after-returning method="afterReturning" pointcut-ref="pc" /> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="pc" /> <!-- 异常拦截通知 --> <aop:after-throwing method="afterException" pointcut-ref="pc"/> <!-- 后置 --> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans>

6.2  采用注解的方式进行配置

个人建议采用注解的方式进行配置,这样配置起来简单,并且需要去掉这个切面的时候也是在同一个类里面去掉,不用再去修改xml文件

package cn.cai.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * aop通知类 * @author 蔡gh_baby * */ @Aspect //用这个注解声明这是一个切面 @Component //将这个类放入spring容器中交个spring管理,相当于xml的<bean name="myAdvice" class="cn.cai.aspect.MyAdvice"></bean> public class MyAdvice_annotation {         /* 标明需要拦截的类的方法,这里表示 userservice 下的所有方法 */ @Pointcut("execution(* cn.cai.service.UserService.*(..) )") public void pointCut(){}        /*         * 前置通知==>目标方法调用前调用 * 后置通知(如果出现异常不会调用)==>目标方法调用后调用 * 环绕通知==>(目标方法调用之前和之后都调用 * 异常拦截通知==>出现异常就调用 * 后置通知(无论是否出现异常都会调用)==>目标方法调用后调用         */ @Before("MyAdvice_annotation.pointCut()")public void before(){System.out.println("这是前置通知");} //后置通知(如果出现异常不会调用)@AfterReturning("pointCut()")public void afterReturnning(){System.out.println("这是后置通知(如果出现异常不会调用)");} //环绕通知@Around("pointCut()")public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{System.out.println("这是环绕通知之前的部分");//调用目标方法Object proceed = proceedingJoinPoint.proceed();System.out.println("这是环绕通知之后的部分");return proceed;} //异常拦截通知@AfterThrowing("pointCut()")public void afterException(){System.out.println("这是异常拦截通知");} //后置通知(无论是否出现异常都会调用)@After("pointCut()")public void after(){System.out.println("后置通知(无论是否出现异常都会调用)");}}

最后测试

package ssm_test.myTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.cai.service.UserService; /** * 使用spring整合junitc测试 * @author 蔡gh_baby * */ //帮我们创建容器 @RunWith(SpringJUnit4ClassRunner.class) //指定创建容器时使用哪个配置文件 @ContextConfiguration({"classpath:spring-web.xml","classpath:spring-mybatis.xml"}) public class MyTest { //将名为user的对象注入到u变量中 @Autowired private UserService userService; @Test public void fun2(){     userService.save(); } }

控制台输出结果:

注意:要打印出下面这个方法的内容需要有异常发生,

//后置通知(如果出现异常不会调用) @AfterReturning("pointCut()") public void afterReturnning(){ System.out.println("这是后置通知(如果出现异常不会调用)"); }

例如在 userservice 的save方法加上这句话

@Override public void save() { System.err.println("保存用户信息"); int a = 1/0; //除数不能为0,因此会抛异常 }

结果:

有不对的请指出

转载请注明原文地址: https://www.6miu.com/read-2623361.html

最新回复(0)