spring AOP的两种配置
- xml配置
- 定义要被代理的方法的接口
public interface TestAop {
public void print(String s);
}
- 实现上述接口
public class TestAopImp implements TestAop{
public void print(String s) {
System.out.println("具体业务逻辑");
}
}
- 定义切面(要在被代理方法的前后进行的操作)
public class LogUtil {
public void logbefore(JoinPoint joinPoint) { //joinPoint为代理的方法
System.out.println("业务处理之前记录日志");
}
public void logAfter(JoinPoint joinPoint) {
System.out.println("业务处理之后记录日志");
}
// @Around("print()")
// public void doAround(ProceedingJoinPoint pjp) throws Throwable {
// System.out.println("开始处理业务");
//// pjp.proceed();
// System.out.println("处理业务结束");
// }
//在处理的过程中发生异常
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:" + e);
}
public void doAfterReturning(Object result) {
System.out.println("后置通知:" + result);
}
}
- xml文件中配置
<bean id="testAop" class="AOP.TestAopImp"/>
<bean id="LogUtil" class="AOP.LogUtil"/>
<aop:config>
<aop:aspect id="aspect" ref="LogUtil">
<aop:pointcut id="PointtestAop" expression="execution(* AOP.TestAopImp.print*(..))"/>
<aop:before method="logbefore" pointcut-ref="PointtestAop"/>
<aop:after method="logAfter" pointcut-ref="PointtestAop"/>
<!--<aop:around method="doAround" pointcut-ref="PointtestAop"/>-->
<aop:after-returning method="doAfterReturning" pointcut-ref="PointtestAop" returning="result"/>
<aop:after-throwing method="doAfterThrowing" throwing="e" pointcut-ref="PointtestAop"/>
</aop:aspect>
</aop:config>
2.注解配置
- 开启注解 在xml配置文件中加上<aop:aspectj-autoproxy/>
- 导包(不导包用不了注解)
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
- 接口和实现类和xml配置相同,接下来定义切面
@Aspect
public class LogUtil {
@Pointcut("execution(* AOP.TestAopImp.print(String))")
public void print() {
} @Before("print()")
public void logbefore(JoinPoint joinPoint) { //joinPoint为代理的方法
System.out.println("业务处理之前记录日志");
} @After("print()")
public void logAfter(JoinPoint joinPoint) {
System.out.println("业务处理之后记录日志");
} // @Around("print()")
// public void doAround(ProceedingJoinPoint pjp) throws Throwable {
// System.out.println("开始处理业务");
//// pjp.proceed();
// System.out.println("处理业务结束");
// } //在处理的过程中发生异常
@AfterThrowing(pointcut = "print()", throwing = "e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:" + e);
} @AfterReturning(pointcut = "print()", returning = "result")
public void doAfterReturning(Object result) {
System.out.println("后置通知:" + result);
}
****************注意点****************
- around切点等于before切点加上after切点,使用的时候二者选其一 pjp.proceed()就等于执行被代理的函数
- 对于几个切点的执行顺序:
try
{
// 执行前置通知;
// 执行目标方法;
// 执行返回通知;
}
catche(Exception e)
{
// 执行异常通知;
}
finally
{
// 执行后置通知;
}
spring AOP的两种配置的更多相关文章
- (一)spring aop的两种配置方式。
sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...
- spring AOP的两种配置方式
连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...
- spring AOP的两种代理
本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理 2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...
- 使用aspectJ实现Spring AOP的两种方式
方式一:基于aspectJ的XML配置 方式二:基于aspectJ的注解方式 基于aspectJ的XML配置 1) 引入相关jar包 2) 创建Spring核心配置文件,必须导 ...
- spring aop的两种写法aspect和advisor
本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...
- spring ----> aop的两种实现方式
实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...
- 学习JavaWeb aop两种配置方式
aop aop:面向切面编程,它可以解决重复代码. aop有两种方式: 一..xml方式 1.在springmvc-servlet.xml中配置aop,应用bean文件: <!--aop配置-- ...
- springmvc配置AOP的两种方式
spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...
- 浅谈Spring的两种配置容器
浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41 作者:黄小鱼ZZZ ...
随机推荐
- jmeter压测学习7-登录参数化(CSV 数据文件设置)
前言 我们在压测登录接口的时候,如果只用一个账号去设置并发压测,这样的结果很显然是不合理的,一个用户并发无法模拟真实的情况. 如果要压测登录接口,肯定得准备几百,甚至上千的账号去登录,测试的结果才具有 ...
- java1.8新特性整理(全)
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yitian_66/article/deta ...
- 代码审计-strpos数组绕过
<?php $flag = "flag"; if (isset ($_GET['ctf'])) { if (@ereg ("^[1-9]+$", $_GE ...
- 【作用域】词法作用域(静态作用域,如:js)、动态作用域(如:.bash脚本)
作用域 作用域是指程序源代码中定义变量的区域. 作用域规定了如何查找变量,也就是确定当前执行代码对变量的访问权限. JavaScript 采用词法作用域(lexical scoping),也就是静态作 ...
- LeetCode237-Delete_Node_In_A_Linked_List
delete-node-in-a-linked-list public void deleteNode(ListNode node) { node.val = node.next.val; node. ...
- shell脚本的输入以及脚本拥有特效地输出
shell脚本的输入以及脚本拥有特效地输出 shell脚本输入之read命令 之前是直接在sh 后加参数 现在是另一种方式 语法:read -参数 -p:给出提示符.默认不支持"\n&quo ...
- JDOJ 3055: Nearest Common Ancestors
JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...
- .net使用IIdentity和IPrincipal实现自定义身份及权限认证【转】
1,通过继承BasePage页实现角色权限控制 context.User中保存的信息就是相关的角色与权限信息.Context.User类型为System.Security.Principal.IPri ...
- [LeetCode] 286. Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...