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(切入点):所谓切 ...
随机推荐
- input 虚拟键盘
if (!Element.prototype.scrollIntoViewIfNeeded) { Element.prototype.scrollIntoViewIfNeeded = function ...
- Html遮罩层的显示(主要在于样式设置)
<html> <head> <title>aaa</title> <script type="text/javascript" ...
- 20145240《网络对抗》逆向及Bof基础实践
逆向及Bof基础实践 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包 ...
- angularjs中的jqlite的认识理解及操作使用
刚了解angularjs时,就知道它有个内嵌的轻量级的jquery:jqLite,那时候常于jQuery分不清,其实它们是不一样的.jqLite中,通过angular.element(param)获得 ...
- iOS开发中的地图开发
显示地图: 1.导入头文件 #import <MapKit/MapKit.h> 如果同时需要用户定位的话还需要 #import <CoreLocation/CoreLocation. ...
- 安装配置mariadb-10.1.19
本文参考:http://chenzehe.iteye.com/blog/1266260 感谢原作者的分享! 首先安装/更新一些编译时会用到的基础包 [root@localhost local]# y ...
- Mac下配置Hadoop环境
下载Hadoop(我下载的是2.8.0版本) 点击此处下载 下载后,使用 tar -zxvf tar包名 解压tar包,解压完成后有这样一个文件出现 修改Hadoop的配置文件 需要修改的配置文件在H ...
- JSon数据类型&使用基础
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ThinkPHP的URL重写时遇到No input file specified的解决方法
因为在Fastcgi模式下,php不支持rewrite的目标网址的PATH_INFO的解析 ThinkPHP运行在URL_MODEL=2时,会出现 No input file specified.的情 ...
- 解决 对路径bin\roslyn..的访问被拒绝
使用visual studio开发,一重新编译就会报错: 对路径“bin\roslyn\System.Reflection.Metadata.dll”的访问被拒绝 一开始的解决办法就是把bin下的文件 ...