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(切入点):所谓切 ...
随机推荐
- qml源码查看
已5.4为例说明: QtQuick源码查看: 地址:Qt\Qt5.4.1\5.4\Src\qtdeclarative\src\quick\items Qt control源码查看: 地址:\Qt\Qt ...
- Yahoo网站性能优化的34条规则
摘自:http://blog.chinaunix.net/uid/20714478/cid-74195-list-1.html Yahoo网站性能优化的34条规则 1.尽量减少HTTP请求次数 终端用 ...
- kali 2.0下搭建DVWA环境
DVWA (Dam Vulnerable Web Application)DVWA是用PHP+Mysql编写的一套用于常规WEB漏洞教学和检测的WEB脆弱性测试程序.包含了SQL注入.XSS.盲注等常 ...
- 20145331 《Java程序设计》第4周学习总结
20145331 <Java程序设计>第4周学习总结 教材学习内容总结 •第六章 1.继承的定义与特点: 子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承方法, ...
- 快用Visual Studio(五)- 语言特性
HTML 自带Emment $ SHIFT + OPTION + F:格式化代码 其他语言特性提示 CSS & LESS hover属性,提示样式对象标签 $ CMD + SHIFT + O: ...
- git-修改远程的URL
git remote set-url命令修改remote URL git remote set-url传递两个参数 remote name.例如,origin或者upstream new remote ...
- 100个gdb调试技巧
找到的一个有参考价值的关于GDB调试的站点:https://gitlore.com/subject/15
- vue.js的一些事件绑定和表单数据双向绑定
知识点: v-on:相当于: 例如:v-on:click==@click ,menthods事件绑定 v-on修饰符可以指定键盘事件 v-model进行表单数据的双向绑定 <template&g ...
- LeetCode——Arithmetic Slices
Question A sequence of number is called arithmetic if it consists of at least three elements and if ...
- 无法读取服务器服务中的服务器队列性能数据。数据段的第一个四字节 (DWORD) 中包
无法打开服务器服务性能对象.数据段的第一个四字节 (DWORD) 包含状态代码. 解决方法:修改注册表禁用PerfNet性能计数器. 具体方法:打开注册表,在HKEY_LOCAL_MACHINE\SY ...