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. 前端隐藏Ios及安卓滚动条

    1.方法不通用 // .scroll_list::-webkit-scrollbar { display:none } .scroll_list::-webkit-scrollbar-track { ...

  2. c++类static成员

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

  3. PHP递归算法

    /** * 获取菜单 * @param number $id * @return multitype: */ public function menu($id = 0) { $menu = M ( ' ...

  4. python 数据分析----matplotlib

    Matplotlib是一个强大的Python绘图和数据可视化的工具包. 安装方法:pip install matplotlib 引用方法:import matplotlib.pyplot as plt ...

  5. 20145314郑凯杰 《Java程序设计》实验一 Java开发环境的熟悉(Windows + Eclipse)实验报告

    20145314郑凯杰 <Java程序设计>实验一 Java开发环境的熟悉(Windows + Eclipse)实验报告 实验要求 •使用JDK编译.运行简单的Java程序: •使用Ecl ...

  6. linux centos7安装phpMyAdmin详解,以及解决各种bug问题

    使用php和mysql开发网站的话,phpmyadmin和navicat是目前非常好的mysql管理工具,但是phpmyadmin最主要是免费开源,目前很多集成的开发环境都会自带phpmyadmin, ...

  7. AccessToken-->Password Grant

    https://www.oauth.com/oauth2-servers/access-tokens/password-grant/ The Password grant is used when t ...

  8. springMvc REST 请求和响应

    前言: 突然怎么也想不起来  springMvc REST 请求的返回  类型了!   (尴尬+究竟)  然后本着 方便的想法 百度了一下 发现了个问题,大家在写      springMvc RES ...

  9. wordpress安装插件提示“wordpress发生意外错误,可能WordPress.org或服务器配置文件存在问题”

    安装wordpress插件,提示报错“wordpress发生意外错误,可能WordPress.org或服务器配置文件存在问题” 出问题的页面是http://*.*.*.*/wp-admin/updat ...

  10. 实现表单checkbox获取已选择的值js代码

    <input type="checkbox" name="cb" value="1" />aa <input type=& ...