Spring中关于AOP的实践之AspectJ方式实现通知

(本文中如有不当之处,恳请批评指正)

AspectJ方式的简化了通知的出现复杂度。但是对配置文件的操作复杂度有了一定的提升

一. 配置通知

 1 package com.xkx.adviceDemo;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 
 5 public class TotalAdvice {
 6 
 7   public void myBefore(){
 8     System.out.println("aspectj方式:物资准备,人员准备,武器准备");
 9   }
10 
11   public void myAfter(){
12     System.out.println("aspectj方式:人员解救成功");
13   }
14 
15   public void myError(){
16     System.out.println("解救出现意外情况,任务执行失败");
17   }
18 
19   public void myArround(ProceedingJoinPoint p){
20     System.out.println("环绕通知:开始解救,准备物资,准备弹药");
21     try {
22       //放行,执行切点方法
23       Object result=p.proceed();
24     } catch (Throwable throwable) {
25       throwable.printStackTrace();
26     }
27     System.out.println("环绕通知:任务执行成功,人质顺利解救");
28 
29   }
30 
31   public void myBeforeHasParameter(String name1,int age1){
32     System.out.println("aspectj方式:"+name1+"    "+age1+"物资准备,人员准备,武器准备");
33   }
34 
35 }

myBeforeHasParameter是一个带有参数的前置通知,其方法参数名要与配置文件中的对应方法完全一致

二.切点所在类的配置

 1 package com.xkx.pojo;
 2 
 3 public class People {
 4   private int age;
 5   private String name;
 6   private String sexual;
 7 
 8   public int getAge() {
 9     return age;
10   }
11 
12   public void setAge(int age) {
13     this.age = age;
14   }
15 
16   public String getName() {
17     return name;
18   }
19 
20   public void setName(String name) {
21     this.name = name;
22   }
23 
24   public String getSexual() {
25     return sexual;
26   }
27 
28   public void setSexual(String sexual) {
29     this.sexual = sexual;
30   }
31 
32 
33 
34   @Override
35   public String toString() {
36     return this.getAge()+"--"+this.getName()+"--"+this.getSexual();
37   }
38 
39   public People() {
40   }
41 
42   public People(int age, String name, String sexual) {
43     this.age = age;
44     this.name = name;
45     this.sexual = sexual;
46 
47   }
48   public void crraped(){
49     System.out.println(this.getName()+"已被绑架");
50   }
51 
52   public void show(){
53     System.out.println("切点执行:解救"+this.getName()+"任务");
54   }
55 
56   public void show2(String name1,int age1){
57     System.out.println("切点执行:解救"+name1+"-->"+age1+"任务");
58   }
59 
60   public void covert(){
61     System.out.println("解救成功,召开新闻发布会");
62   }
63 
64 
65 }

三. 配置Spring的配置文件:applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xmlns:aop="http://www.springframework.org/schema/aop"
 5   xsi:schemaLocation="http://www.springframework.org/schema/beans
 6   http://www.springframework.org/schema/beans/spring-beans.xsd
 7  http://www.springframework.org/schema/aop
 8  http://www.springframework.org/schema/aop/spring-aop.xsd">
 9 
10   <bean id="people2" class="com.xkx.pojo.People"></bean>
11   <bean id="myAdvice" class="com.xkx.adviceDemo.TotalAdvice"></bean>
12 
13   <aop:config>
14     <aop:aspect ref="myAdvice">
15       <aop:pointcut id="mypeople3" expression="execution(* com.xkx.pojo.People.show2(String,int )) and args(name1,age1)"></aop:pointcut>
16       <aop:before method="myBefore" pointcut-ref="mypeople3" ></aop:before>
17       <aop:before method="myBeforeHasParameter" pointcut-ref="mypeople3" arg-names="name1,age1"></aop:before>
18       <aop:after method="myAfter" pointcut-ref="mypeople3"></aop:after>
19       <aop:after-throwing method="myError" pointcut-ref="mypeople3"></aop:after-throwing>
20       <aop:after-returning method="myAfter" pointcut-ref="mypeople3"></aop:after-returning>
21       <aop:around method="myArround" pointcut-ref="mypeople3" ></aop:around>
22     </aop:aspect>
23   </aop:config>
24 </beans>

注意:args()中的参数名可以随意取,没有限制,但是一旦取好后,标红的通知的arg-names的参数就要与前面命名的完全一致,而通知方法的方法参数也要完全同配置文件中的通知参数名完全一致

最后的测试就不贴代码了。

猜你喜欢

转载自www.cnblogs.com/Kaithy-Rookie/p/9783751.html