spring的几个通知(前置、后置、环绕、异常、最终)
2021-07-16 01:04
标签:yar ide main alt location date inf get owa spring的几个通知(前置、后置、环绕、异常、最终) 标签:yar ide main alt location date inf get owa 原文地址:https://www.cnblogs.com/xiaostudy/p/9535857.html1、没有异常的
2、有异常的
1、被代理类接口Person.java
1 package com.xiaostudy;
2
3 /**
4 * @desc 被代理类接口
5 *
6 * @author xiaostudy
7 *
8 */
9 public interface Person {
10
11 public void add();
12 public void update();
13 public void delete();
14 }
2、被代理类PersonImple.java
1 package com.xiaostudy;
2
3 /**
4 * @desc 被代理类
5 *
6 * @author xiaostudy
7 *
8 */
9 public class PersonImple implements Person {
10
11 /**
12 * @desc 实现接口方法
13 */
14 public void add() {
15 System.out.println("add()>>>>>>>>");
16 }
17
18 @Override
19 public void update() {
20 System.out.println("update()>>>>>>>>");
21 // int i = 1/0;
22 }
23
24 @Override
25 public void delete() {
26 System.out.println("delete()>>>>>>>>");
27 }
28
29 }
3、MyAspectJ.java
1 package com.xiaostudy;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.ProceedingJoinPoint;
5
6 /**
7 * @desc 通知类
8 *
9 * @author xiaostudy
10 *
11 */
12 public class MyAspectJ {
13
14 public void myBefort(JoinPoint joinPoint) {
15 System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
16 }
17
18 public void myAfterReturning(JoinPoint joinPoint, Object ret) {
19 System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
20 + ", ret: " + ret);
21 }
22
23 public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
24 System.out.println("环绕通知====前>>>>>>>>>>>");
25 Object obj = joinPoint.proceed();
26 System.out.println("环绕通知====后);
27 return obj;
28 }
29
30 public void myThrowint(JoinPoint joinPoint, Throwable e) {
31 System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
32 + ", e: " + e.getMessage());
33 System.exit(0);
34 }
35
36 public void myAfter(JoinPoint joinPoint) {
37 System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
38 }
39 }
4、spring的配置文件applicationContext.xml
1 2
5、测试类Test.java
1 package com.xiaostudy;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 /**
7 * @desc 测试类
8 *
9 * @author xiaostudy
10 *
11 */
12 public class Test {
13
14 public static void main(String[] args) {
15 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
16 Person person = ac.getBean("person", Person.class);
17 person.add();
18 person.update();
19 person.delete();
20 }
21
22 }
文章标题:spring的几个通知(前置、后置、环绕、异常、最终)
文章链接:http://soscw.com/index.php/essay/105811.html