XML方式实现Spring的AOP
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的更多相关文章
- XML方式实现Spring声明式事务管理
1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...
- Spring AOP(三)--XML方式实现
本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...
- Spring通过XML方式实现定时任务
package com.wisezone.service; import java.text.SimpleDateFormat; import java.util.Date; import org.s ...
- spring总结————AOP面向切面总结
spring总结————AOP面向切面 一.spring aop概念 spring aop面向切面编程,java是面向对象的语言. 真正的service层代码 业务逻辑层再处理业务之前和之后都要进行一 ...
- 04_IOC容器装配Bean(xml方式)
IOC容器装配Bean(xml方式) 1.Spring 提供配置Bean三种实例化方式 1)使用类构造器实例化(默认无参数) <bean id="bean1" class=& ...
- Spring中的AOP注解方式和XML方式
应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型 A. 每种通知的特点和使用方式 B. 获取各种数据,方便日后操作5. 执行表 ...
- Spring之AOP原理、代码、使用详解(XML配置方式)
Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...
- 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)
[AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...
- 6.Spring【AOP】XML方式
1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...
随机推荐
- 20145216史婧瑶《Java 程序设计》第4周学习总结
20145216 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承基本上就是避免多个类间重复定义共同行为. 在Java中,继承时使用exte ...
- Linux下C连接MySql数据库
目录: 一.解决小的问题: 二.大问题,如果你不小心把/usr/lib的所属用户改了导致sudo命令用不了: 三.C连接MySql编程本身: 其实写这个程序真的很简单,十多分钟的事情,只是以前没在Li ...
- 通过Excel生成批量SQL语句
项目中有时会遇到这样的要求:用户给发过来一些数据,要我们直接给存放到数据库里面,有的是Insert,有的是Update等等,少量的数据我们可以采取最原始的办法,也就是在SQL里面用Insert int ...
- ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available
一.笔者使用ifconfig观察网卡情况如下: root@jello:/# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:0 ...
- tomcat监控,自动重启shell脚本
tomcat监控,自动重启shell脚本如下,取名 monitor_tomcat.sh: #!/bin/sh # func:自动监控tomcat脚本并且执行重启操作 # 获取tomcat进程ID(其中 ...
- [译]JavaScript需要类吗?
[译]JavaScript需要类吗? 原文:http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/ 译者注:在 ...
- js 苹果手机 keyup 事件不生效的问题
$(document).on('keyup','input[name="txtInp"]',function(){}) 改 $(document).on('input proper ...
- [Network Architecture]Xception 论文笔记(转)
文章来源 论文:Xception: Deep Learning with Depthwise Separable Convolutions 论文链接:https://arxiv.org/abs/161 ...
- SpringBoot项目结构介绍
一项目结构介绍 springboot框架本身对项目结构并没有特别的要求,但是按照最佳的项目结构可以帮助我们减少可能遇到的错误问题.结构如下: (1)应用主类SpringbootApplication应 ...
- 创建Jmeter中使用的jar包中的工具类方法
1. 在IDEA中新建一个maven工程. 2. 编写工具类方法,如加密算法.此处以加法为例. package DemoTest; public class DemoClass{ public int ...