峰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 ...
随机推荐
- Gym 101147J Whistle's New Car(dfs)
https://vjudge.net/problem/Gym-101147J 题意: 有n个城市,每个城市有一个权值,表示在这个城市的加油站可以加多少油. 现在要计算每个城市i,有多少个城市j可以到达 ...
- Codeforces Round #394 (Div. 2) C.Dasha and Password(暴力)
http://codeforces.com/contest/761/problem/C 题意:给出n个串,每个串的初始光标都位于0(列)处,怎样移动光标能够在凑出密码(每个串的光标位置表示一个密码的字 ...
- python 进制转换
print hex(),hex(-) #转换成十六进制 print oct(),oct(-) #转换成八进制 print bin(),bin(-) #转换成二进制 print int("字面 ...
- spark 累加历史 + 统计全部 + 行转列
spark 累加历史主要用到了窗口函数,而进行全部统计,则需要用到rollup函数 1 应用场景: 1.我们需要统计用户的总使用时长(累加历史) 2.前台展现页面需要对多个维度进行查询,如:产品.地 ...
- Mac上安装配置和简单使用PostgreSQL(仍然很不懂)
因为想要使用推荐的rails-template.需要使用postgres.并初始化了一个用户postgres,密码是postgres.( e.g. $ createuser -d postgres ) ...
- Linux命令详解-echo
echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开,并在最后加上换行号. 1.命令格式: file [ -bchikLnNprsvz ] [ -f namefile ] [ -F se ...
- 剑指offer面试题19 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- Java研发工程师知识点总结
Java研发工程师知识点总结 最近一次更新2017年12月08日 大纲 一.Java基础(语言.集合框架.OOP.设计模式等) 二.Java高级(JavaEE.框架.服务器.工具等) 三.多线程和并发 ...
- 个人知识管理系统Version1.0开发记录(07)
模 块 复 用 原本还要测试一会的,突然出现一连串诡异的问题,比如,编译少加载个类啊,输入地址少个字母啊,改几行代码一改就是半小时啊.这是在提醒我们大脑疲倦了,所以果断小结,下次继续.这一次简单完成了 ...
- xtrabackup备份之增量备份(二)
测试了差不多一周的xtrabackup,本篇主要聊一下xtrabackup的增量备份.我一开始不是很了解,为什么每周都需要做全备份,每天都需要做增量备份,现在想想这个和技术无关.但是有一个新问题,如果 ...