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. 201871010106-丁宣元 《面向对象程序设计(java)》第十四周学习总结

    201871010106-丁宣元 <面向对象程序设计(java)>第十四周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...

  2. 代码审计-md5()函数

    <?php error_reporting(0); $flag = 'flag{test}'; if (isset($_GET['username']) and isset($_GET['pas ...

  3. 12-numpy笔记-莫烦基本操作2

    代码 import numpy as np A = np.arange(3,15) print('-1-') print(A) print('-2-') print(A[3]) A = np.aran ...

  4. 海量数据MySQL项目实战

    主要内容包含 MySQL 典型数据库架构介绍.MySQL 主流数据库架构对比等理论性知识,然后从“订单.用户”两个项目实战,抛砖引玉,介绍亿级互联网业务数据库项目如何设计. MySQL 典型数据库架构 ...

  5. VIJOS-P1064 迎春舞会之数字舞蹈

    洛谷 P1538 迎春舞会之数字舞蹈 洛谷传送门 JDOJ 1245: VIJOS-P1064 迎春舞会之数字舞蹈 JDOJ传送门 Description ​ 在越来越讲究合作的时代,人们注意的更多的 ...

  6. 调试经验分享-让自己的电脑充当WI-Fi模块,用来抓取连接Wi-Fi模块APP上的通信数据

    需求 手头有了厂家的APP和Wi-Fi模块 在已经知道APP是通过TCP连接Wi-Fi模块(8266), 同时也知道了连接的端口号的 情况下如何知道厂家的APP发送给Wi-Fi模块的数据 打开自己的笔 ...

  7. JS通过指定大小来压缩图片

    安装: npm i image-conversion --save 引入: <script src="https://cdn.jsdelivr.net/gh/WangYuLue/ima ...

  8. 不使用xftp上传/下载文件到linux

    yum install lrzsz    # 安装软件 window端上传到linux端: 1. window端先压缩需上传的文件 2. linux端运行命令rz 3. 在弹出的窗口选择压缩好的文件, ...

  9. [探究] $\mu$函数的性质应用

    参考的神仙An_Account的blog,膜一下. 其实就是一类反演问题可以用\(\mu\)函数的性质直接爆算出来. 然后其实性质就是一个代换: \[\sum_{d|n}\mu(d)=[n=1]\] ...

  10. [LeetCode] 900. RLE Iterator RLE迭代器

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...