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. 联想(Lenovo)小新310经典版进bios方法

    1,找到novo按钮. 2,在关机的状态下桶一下小孔,不用任何操作,电脑进入bios选择界面.

  2. c++类static成员

    转自:http://blog.csdn.net/heyabo/article/details/8681516 参考文献:1.http://www.yesky.com/20010828/194000.s ...

  3. RocEDU.阅读.写作《霍乱时期的爱情》书摘(二)

    她不会流一滴眼泪,不会浪费自己的余生,在慢火煮炖的回忆的蛆肉汤中煎熬,不会把自己活活埋葬在四面墙壁之间,成日为自己缝制寿衣,尽管这是当地人乐见寡妇做的事情. 活到这把年纪,人还在的时候就已经腐烂一半了 ...

  4. 20144303 《Java程序设计》第一周学习总结

    20144303 <Java程序设计>第一周学习总结 教材学习内容总结 下载.安装.调试了JDK. JavaSE是各语言个应用平台的基础,分为四个主要的部分:JVE,JRE,JDK,和ja ...

  5. Java 设计模式六原则及23中常用设计模式

    一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接 ...

  6. 学Git,用Git ③

    不知道我前面是否将git讲清楚了,这里再稍微总结一下git的一个重要功能用法,同时增加两个很实用的git使用技巧. 1.git"读档"与git"回退" 我发现我 ...

  7. MongoDB-与Python交互

    与python交互 点击查看官方文档 安装python包 进入虚拟环境 sudo pip install pymongo 或源码安装 python setup.py 引入包pymongo import ...

  8. mybatis映射文件select_resultMap_关联查询_collection定义关联集合

    知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee {    pr ...

  9. LeetCode——Integer Replacement

    Question Given a positive integer n and you can do operations as follow: If n is even, replace n wit ...

  10. MySQL级联删除和级联修改

    1.新建主键table create table demo1_zhujian ( id int primary key auto_increment, name )); 2.新建外键table cre ...