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. 腾讯云,搭建Http静态服务器环境

    任务时间:15min ~ 30min 搭建静态网站,首先需要部署环境.下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务. 安装 Nginx 在 CentOS 上,可直接使 ...

  2. PAT 1145 Hashing - Average Search Time

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  3. i2c中start和restart的区别

    有的硬件芯片提供了一个个寄存器,供我们很好的操作i2c,但是,在用的时候,我们是不知道他到地是怎么操作的,下边,我就探讨下i2c中的start和restart的区别. start是在scl是高电平的时 ...

  4. UVa - 12617 - How Lader

    先上题目:   How Lader  Lader is a game that is played in a regular hexagonal board (all sides equal, all ...

  5. [fw]Best Practices for Exception Handling

    http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html http://www.onjava.com/pub/a/onjava/200 ...

  6. 最小生成树 E - QS Network

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  7. 《Spring in action》之高级装配

    1.Spring 通过配置profile bean.激活profile来设置对应环境. 配置profile bean: 可通过@Profile("dev")注解进行配置.也可以通过 ...

  8. 学PHP也要懂得HTML

    简单的HTML制做: html超文本标记语言 HTML文件主体结构: <!DOCTYPE html><html> <!--htlm开始标记 --><head& ...

  9. spring boot日期转换

    spring boot 作为微服务简易架构.拥有其自身的特点.快速搭建架构 简单 快捷.这里我只是简单的介绍下我遇到的其中的  两个问题.第一前台页面传递的时间类型 无法自动映射到Java的 Date ...

  10. python编程(基于twisted的client编程)

    [ 声明:版权全部,欢迎转载.请勿用于商业用途. 联系信箱:feixiaoxing @163.com] python的twisted比較有意思,既能够做server方面的编程,也能够做client方面 ...