spring 5种通知

方法实现接口
package com.cn.spring.aop.impl;
//加减乘除的接口类
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
接口实现类
package com.cn.spring.aop.impl; import org.springframework.stereotype.Component; //实现类
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cn.spring.aop.impl">
</context:component-scan> <!--使AspjectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
通知切面
package com.cn.spring.aop.impl; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; //把这个类声明为一个切面:首先需要把该类放入到IOC容器中,在声明为一个切面
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(** ArithmeticCalculator.*(..))")
public void declareJointPointExpression(){
}
//声明该方法是一个前置通知:在目标方法开始之前执行
//@Before("execution(public int ArithmeticCalculator.*(int, int))")
//前置通知
@Before("declareJointPointExpression()")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
Object target = joinPoint.getTarget();
System.out.println("The method " + methodName + " begins with " + args + target);
}
//后置通知
@After("execution(** ArithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends ");
}
//返回通知,在方法正常结束后执行的方法,可以访问到方法的返回值
@AfterReturning(value="execution(** ArithmeticCalculator.*(int, int))",
returning = "result")
public void afterReturning(JoinPoint joinPoint , Object result){
String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result);
}
//异常通知
@AfterThrowing(value="execution(** ArithmeticCalculator.*(int, int))",
throwing = "ex")
public void afterThrowing(Exception ex){ System.out.println(ex);
} } /*
* JoinPoint访问到连接点上下文的信息
* JoinPoint
java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;
Signature getSignature() :获取连接点的方法签名对象;
java.lang.Object getTarget() :获取连接点所在的目标对象;
java.lang.Object getThis() :获取代理对象本身;
* */
环绕通知类似于动态代理的全过程
如果环绕通知返回100,则结果为100

//功能最强大的环绕通知
//ProceedingJoinPoint 类型的参数可以决定是否执行目标方法
@Around("execution(** ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null ;
String methodName = pjd.getSignature().getName(); try {
System.out.println("before");
result = pjd.proceed();
System.out.println("after " + result);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Exception " + e);
}
System.out.println("return " + result);
return result;
}
spring 5种通知的更多相关文章
- spring aop的五种通知类型
昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...
- spring aop 的五种通知类型
本文转自:http://blog.csdn.net/cqabl/article/details/46965197 spring aop通知(advice)分成五类: 前置通知[Before advic ...
- Spring AOP 四大通知
Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现 MethodBeforeAdvice 接口,重写 public void before(Method metho ...
- Spring AOP 五大通知类型
1.前置通知 在目标方法执行之前执行执行的通知. 前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象 和 目 ...
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
- SpringAOP使用注解实现5种通知类型
spring aop的5种通知类型都有 Before前置通知 AfterReturning后置通知 Around环绕通知 AfterThrowing异常通知 After最终通知 首先创建接口和实现类 ...
- Spring AOP(通知、连接点、切点、切面)
一.AOP术语 通知(Advice) 切面的工作被称为通知.通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题.5种通知类型: 前置通知(Before): ...
- Java开发学习(十六)----AOP切入点表达式及五种通知类型解析
一.AOP切入点表达式 对于AOP中切入点表达式,总共有三个大的方面,分别是语法格式.通配符和书写技巧. 1.1 语法格式 首先我们先要明确两个概念: 切入点:要进行增强的方法 切入点表达式:要进行增 ...
- spring aop 环绕通知around和其他通知的区别
前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
随机推荐
- 每隔一秒自动执行函数(JavaScript)
http://www.cnblogs.com/xlx0210/archive/2010/03/19/1689497.html 1. setInterval() ——每隔一秒自动执行方法,setInte ...
- 关于tag,viewWithTag
iOS SDK内置了一套搜寻机制,可通过tag来查找子视图. **苹果公司很少给子视图设置tag.笔者所知范围的唯一例外出现在UIAlertView中,该类会给按钮分别设置值为1.2的标签 viewW ...
- 使用runloop阻塞线程的正确写法
使用runloop阻塞线程的正确写法 runloop可以阻塞线程,等待其他线程执行后再执行. 比如: @implementation ViewController{ BOOL end;}…– ( ...
- MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出
先说下本人是菜鸟,通过网上资料学习,终于调通了MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出,以后做静态UI测试就不需要眼睛盯着看图了,这一切交给MonkeyRunner了. 首先 ...
- windows程序设计(二)
MFC架构组成 1.CWinApp的派生类 2.必须在全局区定义一个派生类的对象 3.在CWinApp派生类内必须要有InitInstance虚函数的重写函数 在MFC软件工程以App类中的InitI ...
- cc2530学习笔记
case KEY_CHANGE://按键事件 case AF_INCOMING_MSG_CMD://接收数据事件,调用函数AF_DataRequest()接收数据 case ZDO_STATE_CHA ...
- Git学习 -- 搭建Git服务器
搭建环境 服务器端:CentOS 6.5 IP:192.168.101.129 客户端:CentOS 6.5 . Windows 服务器端: 创建repository版本库,例如/srv/test ...
- L10,not for jazz
expressions: It is called a clavichord这被称为古钢琴 a friend of my father's我父亲的朋友 words: musical,adj,音乐的 ...
- 【记录】haphost免费vps初始配置
1.配置德国epel源 yum install yum-priorities rpm -Uvh http://ftp-stud.hs-esslingen.de/pub/epel/6/i386/epel ...
- 进程waitpid()的用法
代码分析: /* waitpid.c */ #include <sys/types.h> #include <sys/wait.h> #include <unistd.h ...