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. $Python常用内置函数典型用法

    Python中有许多功能丰富的内置函数,本文基于Python 2.7,就常用的一些函数的典型用法做一些积累,不断更新中. sorted函数的三种用法 # coding:utf-8 # sorted函数 ...

  2. java: -source 1.6 中不支持 switch 中存在字符串

    最近在使用IDEA进行单个文件编译的时候给我报错,如题. 解决办法:将 Modules --->Sources ---> Language level 改为 7.0就ok了.

  3. Spring Boot+BootStrap fileInput 多图片上传

    一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...

  4. I.MX6中PC连接开发板问题

    修改板端的文件 添加登录密码: passwd  vi /etc/network/interrfaces 在auto eth0下增加auto eth1 如果采用固定ip方式可以在后面增加一段固定ip设置 ...

  5. 【Deep Learning】两层CNN的MATLAB实现

    想自己动手写一个CNN很久了,论文和代码之间的差距有一个银河系那么大. 在实现两层的CNN之前,首先实现了UFLDL中与CNN有关的作业.然后参考它的代码搭建了一个一层的CNN.最后实现了一个两层的C ...

  6. Python面试题之Python生成器

    首先说明一下生成器也是迭代器,也有迭代器的那些优点. 那为什么要生成器呢?因为到目前为止都 不是你写的迭代器,都是别人定义好的.那如何自己去造一个迭代器呢?下面的内容就会给你答案. 想要自己造一个迭代 ...

  7. saltstack实现自动化扩容

    案例:当nginx的并发达到3000,并持续了一段时间时,通过自动化创建一台虚拟机,部署应用最后添加到集群提供服务: zabbix监控(nginx并发量)------->action------ ...

  8. $.getJSON()函数内的数据不能传到全局变量是怎么回事?

    var json_obj2; $.getJSON("js/invite_panel.json",function(data){ json_obj2=data }) console. ...

  9. 彻底的卸载干净oracle 11g

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭:   2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit>   H ...

  10. centos配置yum源为中国镜像源

    有时候CentOS默认的yum源不一定是国内镜像,导致yum在线安装及更新速度不是很理想.这时候需要将yum源设置为国内镜像站点.国内主要开源的开源镜像站点应该是网易和阿里云了. 修改CentOS默认 ...