峰Spring4学习(6)spring AOP的应用例子
一、AOP简介:

二、AOP实例:

三、使用的例子
需求:在student添加的前后,打印日志信息;
0)spring AOP需要引用的jar包:

1)StudentService.java接口:
package com.cy.service;
public interface StudentService {
public void addStudent(String name);
}
2)StudentServiceImpl.java实现类:
package com.cy.service.impl;
import com.cy.service.StudentService;
public class StudentServiceImpl implements StudentService {
@Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
}
}
3)StudentServiceAspect.java切面类:
package com.cy.advice; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { //前置通知,方法执行之前
public void doBefore(JoinPoint jp){
System.out.println("类名:" + jp.getTarget().getClass().getName());
System.out.println("方法名:" + jp.getSignature().getName());
System.out.println("开始添加学生:" + jp.getArgs()[0]);
} //后置通知 方法完成之后
public void doAfter(JoinPoint jp){
System.out.println("类名:" + jp.getTarget().getClass().getName());
System.out.println("方法名:" + jp.getSignature().getName());
System.out.println("完成添加学生:" + jp.getArgs()[0]);
} //环绕通知 可以在业务方法的前后添加逻辑
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal = pjp.proceed(); //retVal为StudentServiceImpl.addStudent(String name)方法的返回值
//因为返回值为void,所以console打印null
System.out.println("添加学生后");
System.out.println(retVal);
return retVal;
} //返回通知
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
} //异常通知
public void doAfterThrowing(JoinPoint jp, Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:" + ex.getMessage());
}
}
4)beans.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: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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="studentService" class="com.cy.service.impl.StudentServiceImpl"></bean>
<bean id="studentServiceAspect" class="com.cy.advice.StudentServiceAspect"></bean> <aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<aop:pointcut expression="execution(* com.cy.service..*.*(..))" id="businessService"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config> </beans>
5)测试代码:
public class T {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) ac.getBean("studentService");
studentService.addStudent("张三");
}
}
1)没有异常抛出时,没有异常通知;console打印:

2)addStudent方法中构造异常,有异常通知,console打印:
StudentServiceImpl.java:
package com.cy.service.impl;
import com.cy.service.StudentService;
public class StudentServiceImpl implements StudentService {
@Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
System.out.println(1/0);
}
}

doAround方法中抛出异常是这句话:
-- Object retVal = pjp.proceed();
峰Spring4学习(6)spring AOP的应用例子的更多相关文章
- CgLib动态代理学习【Spring AOP基础之一】
如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...
- JavaEE学习之Spring AOP
一.基本概念 AOP——Aspect-Oriented Programming,面向切面编程,它是spring框架的一个重要组成部分.一般的业务逻辑都有先后关系,我们可以理解为纵向关系,而AOP关注的 ...
- Java动态代理学习【Spring AOP基础之一】
Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...
- spring深入学习(四)-----spring aop
AOP概述 aop其实就是面向切面编程,举个例子,比如项目中有n个方法是对外提供http服务的,那么如果我需要对这些http服务进行响应时间的监控,按照传统的方式就是每个方法中添加相应的逻辑,但是这些 ...
- Spring学习十三----------Spring AOP的基本概念
© 版权声明:本文为博主原创文章,转载请注明出处 什么是AOP -面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 -主要的功能是:日志记录.性能统计.安全控制.事务处理. ...
- c# spring aop的简单例子
刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...
- 峰Spring4学习(8)spring对事务的支持
一.事务简介: 二.编程式事务管理: 例子 1.需求:模拟转账,张三向李四转账50元: 数据库中存在t_count表: 代码实现: BankDao.java: package com.cy.dao; ...
- 峰Spring4学习(4)spring自动装配
一.自动装配: Model类: People.java: package com.cy.entity; public class People { private int id; private St ...
- 峰Spring4学习(7)spring对JDBC的支持
第一节: 工程结构: 1)student.java: package com.cy.model; public class Student { private int id; private Stri ...
随机推荐
- 【转载】linux fork死循环炸弹及其预防
转自linux fork死循环炸弹及其预防 在Linux系统下执行这段代码 :(){ :|:& }:: 就会引起死机,一旦执行起来后,唯一的方法就是重启系统.实际上这段代码是一段无限递归代码, ...
- Matplotlib 练习题
1. 绘制一个二维随机漫步的图形 直接上代码: %pylab inline nsteps = 1000 draws = np.random.randint(-1,2,size=(2,nsteps)) ...
- MySQL 5.7.17 Windows安装和启动
1.在官网http://dev.mysql.com/downloads/下载 MySQL Community Server 2.解压后是这个样子(5.7.18解压后没有my-default.ini文件 ...
- Template Method(模板方法)
意图: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.TemplateMethod 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性: 一次性实现一个算法的不变的部分, ...
- Factory Method(工厂方法)
Factory Method(工厂方法) 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method 使一个类的实例化延迟到其子类. 适用性: 当一个类不知道它所必须创 ...
- 网络编程 单机最大tcp连接数
在tcp应用中,server事先在某个固定端口监听,client主动发起连接,经过三路握手后建立tcp连接.那么对单机,其最大并发tcp连接数是多少? 如何标识一个TCP连接 在确定最大连接数之前,先 ...
- UVA-11761-马尔可夫/记忆化搜索
https://vjudge.net/problem/UVA-11762 给出一个整数n,每次随机挑选一个小于等于n的素数,如果是n的因子,n变为n/x ,否则不变,问n变为1的期望挑选次数. f[i ...
- 038——VUE中组件之WEB开发中组件使用场景与定义组件的方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- 在将对象数组转换为json字符串
private List<WHCombineBatchFragmentBarcodeEnterEvent.Message.Data> dataList = new ArrayList< ...