一、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();

最新文章

  1. Android-armebi-v7a、arm64-v8a、armebi的坑
  2. .net 后台读取pdf的值
  3. Geek version acm pc^2 direction for user
  4. Mac下为我们开发的App制作gif动画演示(不仅仅针对开发者,想做gif图片的也可参考)
  5. xcode 最近打开文件列表显示为空或不显示最近打开的项目或(no recent projects)解决办法
  6. DPDK2.1开发者手册4-7
  7. Ubuntu 15.10 x64 安装 Android SDK(转)
  8. GoldenGate 传统抽取进程的 ADG 模式
  9. spring拦截器的简单实现Interceptor
  10. [WinForm]动态显示本地目录图片与悬浮窗
  11. win10下maven的安装与配置
  12. jenkins疑惑
  13. ping内网一台虚拟机延时很大(hyper-v虚拟机)的解决办法
  14. Fib的奇怪定理 : gcd(F[n],F[m])=F[gcd(n,m)]
  15. 判断js数组/对象是否为空
  16. Oracle 12C -- in-database archiving
  17. [UE4]机器人自动寻路
  18. 亚马逊拟斥资15亿美元建航空货运中心 - Amazon to spend $1.49 bln on air cargo hub, fans talk of bigger ambitions - ReutersFebruary 1, 2017
  19. MSSQL代理工作服务器远程命令执行
  20. windows 2008 server R2 服务器docker安装

热门文章

  1. 内核加载模块时提示usb_common: exports duplicate symbol of_usb_get_dr_mode
  2. HDU 6315 Naive Operations(线段树+区间维护)多校题解
  3. 【日志过滤】Nginx日志过滤 使用ngx_log_if不记录特定日志
  4. luogu p1219 八皇后
  5. [fastjson] - fastjson中 JSONObject 和 JSONArray
  6. POJ 2762 Going from u to v or from v to u? (判断单连通)
  7. 前端工程化 - npm
  8. BZOJ 4012 【HNOI2015】 开店
  9. BeginInit与EndInit的实践总结
  10. ubuntu下修改matlab R2016b的快捷键为windows下相同