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) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
随机推荐
- gvim work notes.. a few days' work on 64bit vim and plugin compilations
(a 600MB+ sized c/c++ compiler which is capable of hi-light and JB styled completion!! and of-course ...
- TriggerPrefab 拖拽物体
模拟经营类游戏 有一个特点,就是 拖拽物体.常见的有<帝国><红警><部落战争><凯撒大帝>等等 2d 拖拽 大部分都是 用 OnDrag 方法来 拖动 ...
- c++数组指针bug
ClassA* csList = ]; ClassA ca = csList[]; ca.x=; CCLOG(].x);//output: caList[0].x -431602080.000000 ...
- Windows查看端口被哪个进程占用
命令 查看PID: netstat -ano|findstr 端口号 查看进程名称: tasklist|findstr PID 结束进程: taskkill -F -PID PID号 配图详解: 1. ...
- 【转载】linux环境下为firefox/chrome浏览器安装flash player
本文转载自 http://blog.sina.com.cn/s/blog_6ad624380102v1xf.html firefox安装flash player的方法: 先到adobe网站上下 ...
- 无法加载shockwave flash
热心网友 360浏览器的话,浏览器——工具——选项(非Internet选项)——高级设置——FLASH, 默认使用PPAPI Flash(需要重启浏览器) 默认使用NPAPI Flash(需要重启浏览 ...
- emulator shortcut
Alt+Enter Maximizes the emulator. Ctrl+F11 Changes the orientation of the emulator from landscape to ...
- 正则抓取网页所有href和src
根据抓取的页面,用正则来匹配页面href和src string UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:29.0) Gecko/201001 ...
- cakephp , the subquery
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- MJRefresh
automaticallyChangeAlpha 下拉或上拉时,文字颜色逐渐加深