注解配置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 ...
随机推荐
- plot3d
plot3d plot3D a format for structured grid data that was popularized by NASA's CFD visualization Aut ...
- (一)U-Boot启动过程--详细版的完全分析
博客地址:http://blog.csdn.net/hare_lee/article/details/6916325
- 51nod1429 巧克力
[题解] 找一下两个面积s1,s2的2和3的因子数,把他们除掉,如果s1,s2不相等,就是-1,否则可以用s1,s2的2和3的因子数计算答案. #include<cstdio> #incl ...
- Ajax_使用 jQuery 实现Ajax
[jQuery中的Ajax] 1.jQuery对Ajax操作进行了封装,在jQuery中最底层的方法时 $.ajax().第二层是 load() , $.get() 和 $.post(),第三层是 ...
- POJ 1970 The Game
The Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6886 Accepted: 1763 Descript ...
- vue2源码浏览分析02
1.组件初始化方法 init Vue.prototype._init = function (options) { /* istanbul ignore if */ if ("develop ...
- ssh连接断开后 shell进程退出
问题描述:当SSH远程连接到服务器上,然后运行一个服务 ./catalina.sh start,然后把终端开闭(切断SSH连接)之后,发现该服务中断,导致网页无法访问. 解决方法:使用nohup命 ...
- android 请求网络异步载入
/** * 封装ProecssDialog对话框 * */ public class LoadDialog extends ProgressDialog { private String title ...
- 利用FFmpge进行视频压缩(从图像到H264视频流)
对于FFmpeg相信做视频或图像处理这一块的都不会陌生,在网上也能找到非常多相关的代码.但因为版本号不同等原因.往往找到的代码都是须要自行改动才干够用,为此本人希望能尽绵薄之力,将开发包和自行编写的代 ...
- 苹果app审核,怎样申请加急审核
苹果app加急审核,是为开发人员提供的特殊通道. 当线上有重大问题须要解决时,能够提出加急审核申请. 心急的朋友直接点 传送门 那么加急审核的入口在哪里呢 首先打开itunesconnect管理后台 ...