1. xml配置
  • 定义要被代理的方法的接口
public interface TestAop {
public void print(String s);
}
  • 实现上述接口
public class TestAopImp implements TestAop{
public void print(String s) {
System.out.println("具体业务逻辑");
}
}
  • 定义切面(要在被代理方法的前后进行的操作)
public class LogUtil {
public void logbefore(JoinPoint joinPoint) { //joinPoint为代理的方法
System.out.println("业务处理之前记录日志");
} public void logAfter(JoinPoint joinPoint) {
System.out.println("业务处理之后记录日志");
} // @Around("print()")
// public void doAround(ProceedingJoinPoint pjp) throws Throwable {
// System.out.println("开始处理业务");
//// pjp.proceed();
// System.out.println("处理业务结束");
// } //在处理的过程中发生异常
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:" + e);
} public void doAfterReturning(Object result) {
System.out.println("后置通知:" + result);
}
}
  • xml文件中配置
 <bean id="testAop" class="AOP.TestAopImp"/>
<bean id="LogUtil" class="AOP.LogUtil"/>
<aop:config>
<aop:aspect id="aspect" ref="LogUtil">
<aop:pointcut id="PointtestAop" expression="execution(* AOP.TestAopImp.print*(..))"/>
<aop:before method="logbefore" pointcut-ref="PointtestAop"/>
<aop:after method="logAfter" pointcut-ref="PointtestAop"/>
<!--<aop:around method="doAround" pointcut-ref="PointtestAop"/>-->
<aop:after-returning method="doAfterReturning" pointcut-ref="PointtestAop" returning="result"/>
<aop:after-throwing method="doAfterThrowing" throwing="e" pointcut-ref="PointtestAop"/>
</aop:aspect>
</aop:config>

  2.注解配置

  • 开启注解 在xml配置文件中加上<aop:aspectj-autoproxy/>
  • 导包(不导包用不了注解)
    <dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
  • 接口和实现类和xml配置相同,接下来定义切面
@Aspect
public class LogUtil {
@Pointcut("execution(* AOP.TestAopImp.print(String))")
public void print() {
} @Before("print()")
public void logbefore(JoinPoint joinPoint) { //joinPoint为代理的方法
System.out.println("业务处理之前记录日志");
} @After("print()")
public void logAfter(JoinPoint joinPoint) {
System.out.println("业务处理之后记录日志");
} // @Around("print()")
// public void doAround(ProceedingJoinPoint pjp) throws Throwable {
// System.out.println("开始处理业务");
//// pjp.proceed();
// System.out.println("处理业务结束");
// } //在处理的过程中发生异常
@AfterThrowing(pointcut = "print()", throwing = "e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:" + e);
} @AfterReturning(pointcut = "print()", returning = "result")
public void doAfterReturning(Object result) {
System.out.println("后置通知:" + result);
}

****************注意点****************

  1. around切点等于before切点加上after切点,使用的时候二者选其一 pjp.proceed()就等于执行被代理的函数
  2. 对于几个切点的执行顺序:
     try
    {
        //  执行前置通知;
        
        //  执行目标方法;
        
        // 执行返回通知;
    }
    catche(Exception e)
    {
        // 执行异常通知;
    }
    finally
    {
        // 执行后置通知;
    }

spring AOP的两种配置的更多相关文章

  1. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  2. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  3. spring AOP的两种代理

    本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理  2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...

  4. 使用aspectJ实现Spring AOP的两种方式

    方式一:基于aspectJ的XML配置 方式二:基于aspectJ的注解方式 基于aspectJ的XML配置 1)       引入相关jar包 2)       创建Spring核心配置文件,必须导 ...

  5. spring aop的两种写法aspect和advisor

    本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...

  6. spring ----> aop的两种实现方式

    实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...

  7. 学习JavaWeb aop两种配置方式

    aop aop:面向切面编程,它可以解决重复代码. aop有两种方式: 一..xml方式 1.在springmvc-servlet.xml中配置aop,应用bean文件: <!--aop配置-- ...

  8. springmvc配置AOP的两种方式

    spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...

  9. 浅谈Spring的两种配置容器

    浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41   作者:黄小鱼ZZZ     ...

随机推荐

  1. Ubuntu下搭建Kubernetes集群(3)--k8s部署

    1. 关闭swap并关闭防火墙 首先,我们需要先关闭swap和防火墙,否则在安装Kubernetes时会导致不成功: # 临时关闭 swapoff -a # 编辑/etc/fstab,注释掉包含swa ...

  2. error: exportArchive: The data couldn’t be read because it isn’t in the correct format.

    在执行ios 打包的时候,我们通过执行下面的指令来打包ipa: mkdir arch archive_path=arch/${app_name}.xcarchive workspace_name=HP ...

  3. pdfium sdk调用方式

    FPDF_InitLibrary(NULL); FPDF_CreateNewDocument(); FPDF_DestroyLibrary();

  4. 02-人脸识别-基于MTCNN,框选人脸区域-detect_face

    (本系列随笔持续更新) 这部分代码是基于参考中的链接,修改后适用于TensorFlow1.6.0版本的代码.由于TensorFlow的频繁更新,所以不一定支持后续新或者就版本,特此说明. 程序的最初版 ...

  5. python预科前三天:计算器知识、Python下载和安装、Pycharm下载安装激活设置、解释型和编译型、git、思维导图、显示隐藏文件、隐藏已知文件扩展名、创建组织、创建项目、提交作业、排BUG技巧

    1.计算机组成结构:CPU.硬盘.内存.输入输出设备.主板.电源. 2.硬件之间的协作关系:是CPU运算完后给操作系统.专业术语叫指令. 3.键盘输入a之后发生的事情:键盘-CPU-操作系统-显卡-显 ...

  6. CPU-bound(计算密集型) 和I/O bound(I/O密集型)/数据密集型

    https://blog.csdn.net/q_l_s/article/details/51538039 I/O密集型 (CPU-bound)I/O bound 指的是系统的CPU效能相对硬盘/内存的 ...

  7. P256 VRF实现解读

    目录 P256 VRF实现及其改造 公式推导 H1:把任意信息映射到曲线上的点 H2: 映射任意信息为(1,q) 计算随机数 随机数的proof 如何验证 VRF优点 针对S256曲线的改造 1. 使 ...

  8. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  9. AtCoder Grand Contest 037题解

    传送门 \(A\) 直接把每个字母作为一个字符串,如果某个串和它前面的相同,那么就把这个字母和它后面那个字母接起来.然而我并不会证明这个贪心的正确性 //quming #include<bits ...

  10. MySQL学习笔记2————基础篇记录

    这里以实验楼的数据库来记录,如有侵犯实验楼权益,请联系本人,必定删除 在此感谢实验楼提供的免费教程 MySQL 基础课程_SQL - 实验楼 一. 表project employee 任务:想要知道名 ...