1、编写切面类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.fz.annotation.aop;
 
import org.aspectj.lang.ProceedingJoinPoint;
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;
 
public class LogInterceptor {
     
    public void before(){
        System.out.println("方法之前执行....");
    }
    public void afterRutting(){
        System.out.println("方法正常执行之后....");
    }
    public void afterThrowing(){
        System.out.println("方法抛出异常之后....");
    }
    public void around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("方法执之前around......");
        pjp.proceed();//向下继续方法的执行:(包括执行其他切面的拦截,如果当中抛出异常,则不会向下继续执行)
        System.out.println("方法执之后around......");//这里会在@AfterReturning执行之后执行
    }
}

2、编写applicationContext.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    <context:annotation-config />
    <context:component-scan base-package="com.fz.annotation" />
    <bean id="logInterceptor" class="com.fz.annotation.aop.LogInterceptor">
    </bean>
    <aop:config>
        <aop:pointcut
            expression="execution(public void com.fz.annotation.service.impl.UserServiceImpl.userAdd(com.fz.xml.entity.User))"
            id="servicePointcut" />
        <aop:aspect id="logAspect" ref="logInterceptor">
            <aop:before method="before" pointcut-ref="servicePointcut" />
        </aop:aspect>
    </aop:config>
     
</beans>

其中:<aop:pointcut>标签和<aop:aspect>同级,表示<aop:pointcut>是一个全局的pointcut

也可以让pointcut只供某一个逻辑使用,像下面这样写

1
2
3
4
5
6
7
<aop:config>
    <aop:aspect id="logAspect" ref="logInterceptor">
         <aop:pointcut id="servicePointcut"
        expression="execution(public void com.fz.annotation.service.impl.UserServiceImpl.userAdd(com.fz.xml.entity.User))"/>        
        <aop:before method="before" pointcut-ref="servicePointcut" />
    </aop:aspect>
</aop:config>

还有可以这样写,注意:直接在before里写pointcut属性的时候需要将pointcut-ref属性去掉

1
2
3
4
5
<aop:config>
    <aop:aspect id="logAspect" ref="logInterceptor">
        <aop:before method="before" pointcut="execution(public void com.fz.annotation.service.impl.UserServiceImpl.userAdd(com.fz.xml.entity.User))"/>
    </aop:aspect>
</aop:config>

XML方式实现Spring的AOP的更多相关文章

  1. XML方式实现Spring声明式事务管理

    1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...

  2. Spring AOP(三)--XML方式实现

    本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...

  3. Spring通过XML方式实现定时任务

    package com.wisezone.service; import java.text.SimpleDateFormat; import java.util.Date; import org.s ...

  4. spring总结————AOP面向切面总结

    spring总结————AOP面向切面 一.spring aop概念 spring aop面向切面编程,java是面向对象的语言. 真正的service层代码 业务逻辑层再处理业务之前和之后都要进行一 ...

  5. 04_IOC容器装配Bean(xml方式)

    IOC容器装配Bean(xml方式) 1.Spring 提供配置Bean三种实例化方式 1)使用类构造器实例化(默认无参数) <bean id="bean1" class=& ...

  6. Spring中的AOP注解方式和XML方式

    应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型     A. 每种通知的特点和使用方式    B. 获取各种数据,方便日后操作5. 执行表 ...

  7. Spring之AOP原理、代码、使用详解(XML配置方式)

    Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...

  8. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  9. 6.Spring【AOP】XML方式

    1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...

随机推荐

  1. Java Hashtable详细介绍和使用示例

    ①对Hashtable有个整体认识 和HashMap一样,Hashtable 也是一个散列表,它存储的内容是键值对(key-value)映射.Hashtable 继承于Dictionary,实现了Ma ...

  2. SpringBoot Web项目中中如何使用Junit

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...

  3. ARKit 研究笔记一

    软件需求:Xcode9.x .blender 硬件需求:iphone 6s + 系统:iOS 11 + 技能储备: ARKit .SceneKit(苹果提供的3d游戏库) 或 SpriteKit(苹果 ...

  4. Android模拟器Intel Atom下载安装配置

    https://software.intel.com 在Android x86模拟器Intel Atom x86 System Image时提示Intel execute disable bit(xd ...

  5. JAVA面试题整理(1)-基础

    1.List 和 Set 的区别  共同点:它们都是Collection的子接口 区别: List:这个接口能够精准的记录每一个元素的插入位置(换句话说就是这个接口内容所有元素是按照顺序去保存的),使 ...

  6. 游戏服务器的思考之三:谈谈MVC

    游戏服务器也是基于MVC架构的吗?是的,所有的应用系统都是基于MVC架构的,这是应用系统的天性.不管是客户端还是后台,都包含模型.流程.界面这3个基本要素:不同类型的应用,3要素的“重量”可能各有偏差 ...

  7. 建站有很多技术,如 HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、http://ASP.NET、Web Services、浏览器脚本、服务器脚本等。它们的区别是什么?新手一点不懂,想理清所有这些技术之间的关系和应用范围。

    先普及用户通过 浏览器 访问网页 的过程: 网页内容是通过服务器运算得出的结果,将结果(网页代码)传输给浏览器,网页代码再通过浏览器运算(计算.渲染),最终展示在用户的眼前的. 至此,我们知道了有2个 ...

  8. 【bzoj4423】[AMPPZ2013]Bytehattan(平面图转对偶图+并查集)

    题目传送门:bzoj4423 如果是普通的删边判连通性,我们可以很显然的想到把操作离线下来,倒着加边.然而,这题强 制 在 线. 虽然如此,但是题目所给的图是个平面图.那么我们把它转成对偶图试试看? ...

  9. 搭建时间服务器(linux)

    我们搭建集群环境的时候,时间必须是要统一的,才能保证集群数据的一致性. 一般操作是直接使用NTP,跟默认的时间服务器同步,但是最好还是让所有节点跟集群中的某台作为时间服务器的节点同步. 步骤:(节点有 ...

  10. php调用mysql存储过程游标

    <?php $dbtype = 'mysql'; $host = 'localhost'; $dbname = 'test'; $dsn = "$dbtype:host=$host;d ...