1、没有异常的

2、有异常的


1、被代理类接口Person.java

 package com.xiaostudy;

 /**
* @desc 被代理类接口
*
* @author xiaostudy
*
*/
public interface Person { public void add();
public void update();
public void delete();
}

2、被代理类PersonImple.java

 package com.xiaostudy;

 /**
* @desc 被代理类
*
* @author xiaostudy
*
*/
public class PersonImple implements Person { /**
* @desc 实现接口方法
*/
public void add() {
System.out.println("add()>>>>>>>>");
} @Override
public void update() {
System.out.println("update()>>>>>>>>");
// int i = 1/0;
} @Override
public void delete() {
System.out.println("delete()>>>>>>>>");
} }

3、MyAspectJ.java

 package com.xiaostudy;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* @desc 通知类
*
* @author xiaostudy
*
*/
public class MyAspectJ { public void myBefort(JoinPoint joinPoint) {
System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
} public void myAfterReturning(JoinPoint joinPoint, Object ret) {
System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", ret: " + ret);
} public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知====前>>>>>>>>>>>");
Object obj = joinPoint.proceed();
System.out.println("环绕通知====后<<<<<<<<<<<");
return obj;
} public void myThrowint(JoinPoint joinPoint, Throwable e) {
System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", e: " + e.getMessage());
System.exit(0);
} public void myAfter(JoinPoint joinPoint) {
System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
}
}

4、spring的配置文件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: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.xsd">
<!-- 创建被代理类 -->
<bean id="person" class="com.xiaostudy.PersonImple"></bean>
<!-- 创建切面类 -->
<bean id="advice" class="com.xiaostudy.MyAspectJ"></bean>
<!-- springAOP编程 -->
<aop:config>
<!-- 将切面类 声明“切面”,从而获得通知(方法) -->
<aop:aspect ref="advice">
<!-- 声明一个切入点,所有的通知都可以使用 -->
<aop:pointcut expression="execution(* com.xiaostudy.PersonImple.*(..))" id="myPointcut"/>
<!-- 前置通知: method表示:方法名,pointcut-ref表示:所有的通知共享,(pointcut表示:只有当前通知可用,其他的不能用) -->
<aop:before method="myBefort" pointcut-ref="myPointcut"/>
<!-- 后置通知:returning表示:后置通知的第二个参数名,内容是方法的返回值 -->
<aop:after-returning method="myAfterReturning" returning="ret" pointcut-ref="myPointcut"/>
<!-- 环绕通知 -->
<aop:around method="myAround" pointcut-ref="myPointcut"/>
<!-- 异常通知:throwing表示:异常通知的第二个参数,内容是异常信息 -->
<aop:after-throwing method="myThrowint" throwing="e" pointcut-ref="myPointcut"/>
<!-- 最终通知 -->
<aop:after method="myAfter" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>

5、测试类Test.java

 package com.xiaostudy;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @desc 测试类
*
* @author xiaostudy
*
*/
public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = ac.getBean("person", Person.class);
person.add();
person.update();
person.delete();
} }

最新文章

  1. html_随笔
  2. VC++/MFC 最常用宏和指令
  3. mySql 基本语法学习笔记
  4. W3School-CSS 文本实例
  5. javascript_获取iframe框架中元素节点的属性值
  6. JS方式调用本地的可执行文件
  7. ES5中的有9个Array方法
  8. TesserOCR训练
  9. JSONP技术原理及实现
  10. 如何通过WiFi来进行Android的真机模拟
  11. CentOS 7 修改hostname
  12. LintCode-比较字符串
  13. asp.net在用户控件中使用ClientScript
  14. Saltstack的部署及其详解
  15. Hadoop 3.1.2(HA)+Zookeeper3.4.13+Hbase1.4.9(HA)+Hive2.3.4+Spark2.4.0(HA)高可用集群搭建
  16. uniDAC的安装和使用
  17. React Native原生模块向JS传递数据的几种方式(Android)
  18. DOM心得
  19. 可以替代alert 的漂亮的Js弹框
  20. Python自动化开发 - 常用模块(一)

热门文章

  1. iOS 如何用JSONKit读写JSON文件
  2. 【Charles】使用教程+破解+Windows版本https乱码+https证书安装注意
  3. python得到一个10位随机数的方法及拓展
  4. 关于jQuery中nth-child和nth-of-type的详解
  5. 关于Nginx部署Django项目的资料收集
  6. Pandas 通过追加方式合并多个csv
  7. django博客项目9
  8. Flask(5)- Flask-Session组件、WTForms组件、数据库连接池(POOL)
  9. upsampling(上采样)&amp; downsampled(降采样)
  10. sql 使用select 生成json