注解配置AOP切面编程
1、导入先关jar包
2、编写applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <context:component-scan base-package="com.wh.aop"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
3、编写被切的类
package com.wh.aop; import org.springframework.stereotype.Component; @Component
public class Computer { public void play01(){
System.out.println("play01玩家");
} public String play02(){
System.out.println("play02玩家");
return "play02";
} public String play03(){
System.out.println("play03玩家");
return "play03"+(10/0);
} public String play04(){
System.out.println("play04玩家");
return "play04";
} }
4、编写切面类
package com.wh.aop; 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.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
@Aspect
public class AopProxy { @Pointcut("execution(* com.wh.aop.Computer.play01(..))")
public void cp(){} @Before("cp()")
public void doBefore(JoinPoint p){
System.out.println("前置通知!");
} @AfterReturning(value="execution(* com.wh.aop.Computer.play02())",returning="result")
public void doAfterReturning(JoinPoint p,Object result){
System.out.println("后置通知 "+result);
} @After("execution(* com.wh.aop.Computer.play02())")
public void doAfter(JoinPoint p){
System.out.println("最终通知 ");
} @AfterThrowing(value="execution(* com.wh.aop.Computer.play03())",throwing="e")
public void doAfterThrowing(JoinPoint p,Throwable e){
System.out.println("异常通知: "+e);
} @Around("execution(* com.wh.aop.Computer.play04())")
public void doAround(ProceedingJoinPoint p){
System.out.println("前置通知");
Object obj=null;
try {
obj=p.proceed();
} catch (Throwable e) {
System.out.println("异常通知: "+e.getMessage());
}finally{
System.out.println("最终通知!");
}
System.out.println("后置通知!"+obj);
} }
5、编写测试类
package com.wh.aop; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test
public void testdoBefore(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer");
c.play01();
}
/*
前置通知!
play01玩家
*/ @Test
public void testdoAfterReturning(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer");
c.play02();
}
/*
play02玩家
后置通知 play02
*/ @Test
public void testdoAfter(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer");
c.play02();
}
/*
play02玩家
最终通知
后置通知 play02
*/ @Test
public void testdoAfterThrowing(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer");
c.play03();
}
/*
play03玩家
异常通知: java.lang.ArithmeticException: / by zero
*/ @Test
public void testdoAround(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer");
c.play04();
}
/*
前置通知
play04玩家
最终通知!
后置通知!play04
*/ }
注解配置AOP切面编程的更多相关文章
- SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...
- 注解与AOP切面编程实现redis缓存与数据库查询的解耦
一般缓存与数据库的配合使用是这样的. 1.查询缓存中是否有数据. 2.缓存中无数据,查询数据库. 3.把数据库数据插入到缓存中. 其实我们发现 1,3 都是固定的套路,只有2 是真正的业务代码.我们可 ...
- SpringBoot 通过自定义注解实现AOP切面编程实例
一直心心念的想写一篇关于AOP切面实例的博文,拖更了许久之后,今天终于着手下笔将其完成. 基础概念 1.切面(Aspect) 首先要理解‘切’字,需要把对象想象成一个立方体,传统的面向对象变成思维,类 ...
- applicationContext.xml配置AOP切面编程
Computer.java package com.wh.aop2; public class Computer { public void play01(){ System.out.println( ...
- Spring MVC通过AOP切面编程 来拦截controller 实现日志的写入
首选需要参考的是:[参考]http://www.cnblogs.com/guokai870510826/p/5977948.html http://www.cnblogs.com/guokai8 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- AOP切面编程在android上的应用
代码地址如下:http://www.demodashi.com/demo/12563.html 前言 切面编程一直是一个热点的话题,这篇文章讲讲一个第三方aop库在android上的应用.第三方AOP ...
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- spring-AOP框架(基于AspectJ注解配置AOP)
基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...
随机推荐
- mac 中查看监听程序
sudo lsof -nP -iTCP -sTCP:LISTEN | grep mysql
- NT9666X调试log
1.给GSensor_open();前加上打印函数DEBUG_P;打印如下信息: ######## FILE = e:/Project_code/Philips_PanGu/Philips_PanGu ...
- 【C++】实现记录软件计时时间
利用getTickCount()和getTickFrequency()函数实现计时 double time0 = static_cast<double>(getTickCount()); ...
- yarn & vue-cli
yarn & vue-cli https://github.com/xgqfrms/ES6/issues/10 install https://yarnpkg.com/zh-Hans/docs ...
- 校长的收藏(洛谷 U4534)
题目背景 XS中学的校长喜欢收集手办,家里面都是价值不菲的手办. 校长喜欢给手办们排队并且对于某些些区间内的手办喜爱有加. 现在,校长外出散步(找乐子),你潜入他的房间打算借(偷走)他的手办炫耀一下. ...
- MySQL主从复制邮件报警脚本
#!/bin/shexport PATH=$PATH:/application/mysql/3306/binlogFile=`date +"%Y-%m-%d %H:%M:%S"`_ ...
- Ubuntu 16.04安装Meld文件比对工具替代Beyond Compare
Beyond Compare是商业软件,不建议使用,下载地址:http://www.scootersoftware.com/download.php.下载完直接运行或者通过dpkg安装即可. 其实Li ...
- 3.5 在批处理模式下使用mysql
在前面的章节中,你交互式地使用mysql输入查询而且查看结果.你也能够以批模式执行mysql.为了做到这些.把你想要执行的命令放在一个文件里,然后告诉mysql从文件读取它的输入: shell> ...
- JS执行环境栈及事件循环机制—简洁明了的讲解
JavaScript解释器在浏览器中是单线程的,这意味着浏览器在同一时间内只执行一个事件,对于其他的事件我们把它们排队在一个称为 执行栈(调用栈) 的地方.下表是一个单线程栈的抽象视图: 我们已经知道 ...
- Oracle 闪回区满解决的方法
闪回区满: OS: rm -rf [archivelog autobackup backupset controlfile flashback onlinelog] eg : archive ...