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切面编程的更多相关文章

  1. SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...

  2. 注解与AOP切面编程实现redis缓存与数据库查询的解耦

    一般缓存与数据库的配合使用是这样的. 1.查询缓存中是否有数据. 2.缓存中无数据,查询数据库. 3.把数据库数据插入到缓存中. 其实我们发现 1,3 都是固定的套路,只有2 是真正的业务代码.我们可 ...

  3. SpringBoot 通过自定义注解实现AOP切面编程实例

    一直心心念的想写一篇关于AOP切面实例的博文,拖更了许久之后,今天终于着手下笔将其完成. 基础概念 1.切面(Aspect) 首先要理解‘切’字,需要把对象想象成一个立方体,传统的面向对象变成思维,类 ...

  4. applicationContext.xml配置AOP切面编程

    Computer.java package com.wh.aop2; public class Computer { public void play01(){ System.out.println( ...

  5. Spring MVC通过AOP切面编程 来拦截controller 实现日志的写入

    首选需要参考的是:[参考]http://www.cnblogs.com/guokai870510826/p/5977948.html    http://www.cnblogs.com/guokai8 ...

  6. Spring AOP 切面编程记录日志和接口执行时间

    最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...

  7. AOP切面编程在android上的应用

    代码地址如下:http://www.demodashi.com/demo/12563.html 前言 切面编程一直是一个热点的话题,这篇文章讲讲一个第三方aop库在android上的应用.第三方AOP ...

  8. 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  9. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

随机推荐

  1. LINUX-DEB 包 (Debian, Ubuntu 以及类似系统)

    dpkg -i package.deb 安装/更新一个 deb 包 dpkg -r package_name 从系统删除一个 deb 包 dpkg -l 显示系统中所有已经安装的 deb 包 dpkg ...

  2. 关于字符串不为空 错误:s!=null

    错误:s!=null 正确:StringUtils.isNotBlank(s); public static boolean isBlank(CharSequence cs) { int strLen ...

  3. Caffe——清晰高效的深度学习(Deep Learning)框架

    Caffe(http://caffe.berkeleyvision.org/)是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清(http://daggerfs.com/ ...

  4. Linux声卡驱动移植和測试

    一.分析驱动程序,依据开发板改动代码 代码太长,就不贴了,几个注意点: 1. 查看开发板原理图和S3C2410的datasheet,UDA1341的L3MODE.L3DATA.L3CLOCK分别与S3 ...

  5. leetCode(49):Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 推断一个数是否是质数主要有下面几种 ...

  6. 《C# 6.0 本质论》 阅读笔记

    <C# 6.0 本质论> 阅读笔记   阅读笔记不是讲述这本书的内容,只是提取了其中一部分我认为比较重要或者还没有掌握的知识,所以如果有错误或者模糊之处,请指正,谢谢! 对于C# 6.0才 ...

  7. 解决华为手机不出现logcat日志的问题

    问题描写叙述:公司一部华为手机在连接Eclipse时在Logcat中看不到相关日志 解决方法:1 进入手机拨号界面2 输入*#*#2846579#*#*3 输入完成后自己主动跳转到測试界面4 依次选择 ...

  8. Kafka无法消费!?究竟是bug的“沦陷”还是配置的“扭曲”?

    在一个月黑风高的夜晚,突然收到现网生产环境Kafka消息积压的告警,梦中惊醒啊,马上起来排查日志. 问题现象 消费请求卡死在查找Coordinator Coordinator为何物?Coordinat ...

  9. C++中的继承与虚函数各种概念

    虚继承与一般继承 虚继承和一般的继承不同,一般的继承,在目前大多数的C++编译器实现的对象模型中,派生类对象会直接包含基类对象的字段.而虚继承的情况,派生类对象不会直接包含基类对象的字段,而是通过一个 ...

  10. 【Unity 3D】学习笔记三十六:物理引擎——刚体

    物理引擎就是游戏中模拟真是的物理效果.如两个物体发生碰撞,物体自由落体等.在unity中使用的是NVIDIA的physX,它渲染的游戏画面很逼真. 刚体 刚体是一个很很中要的组件. 默认情况下,新创的 ...