峰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 ...
随机推荐
- Scan法求凸包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 给一个半径和n个点 求圆的周长 + n个点的凸包的周长 #include<bits/std ...
- c#pdf查看器
Free Spire.PDF for .NET is a Community Edition of the Spire.PDF for .NET, which is a totally free PD ...
- 国内maven库镜像(阿里云)
我觉得fuck GFW(Great FireWall) 真是阻碍国内技术发展罪大恶极的东西.各种不方便,各种落后,各种闭塞. anyway,maven中央仓库,本来有oschina的可以用,现在关了. ...
- Solaris 11 让 ls 的输出 带上颜色
Solaris 默认的ls , 是不会显示 文件和文件夹的颜色的. 我们可以利用 gnu 的 ls 命令. 修改:~/.bashrc alias ls='/usr/gnu/bin/ls --color ...
- Eclipse开发Android应用 找不到平板
1.驱动安装正确2.平板的连接方式正确,不要用大容量存储/sd卡模式这个设置在4.3上很难找呀.设置->存储->点右上角的菜单 3.打开USB调试.4.上述问题都检查后,在eclipse里 ...
- OKR 说明
转载来源: http://www.jianshu.com/p/ce1141084427 一.什么是OKR? OKR的全称是“Objectives and Key Results”,翻译过来就是“目标和 ...
- Learn Rails5.2 Bundler ; Forms
如果一个Rubyer想要提供一个功能或某个程序或程序的集合给其他Rubyer使用,这个Rubyer可以创建一个package,这个package就叫做gems. 可以通过gem install安装. ...
- [linux]文件系统损坏,linux启动时 checking filesystems fail
先敲root password进入maintenance状态,然后fsck -y /dev/mapper/vg_wwwdata-lv_root等干净了以后,再exit就行了. ------------ ...
- UVA-1343 The Rotation Game (IDA*)
题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转 ...
- C#接口作用
1.C#接口的作用 : C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正 ...