切⾯编程(环绕通知与前后置通知区别)
解决问题
1、拥有前置通知和后置通知的功能,并能解决前置通知和后置通知在共享信息⽅⾯的不⾜(例如:统计切点⽅法执⾏时间);
2、在多线程并发条件下,能保证线程安全(因为在⼀个⽅法内定义的局部变量);
3、解决代码重复性,降低代码复杂程度;
内容说明
1、以下会给出前置通知、后置通知与环绕通知实例(观众观看表演),通过对⽐更能理解彼此之间的区别;
2、两者都通过@Component注解,扫描(Audience,Juggler)bean并注册到spring容器中时,需在XML配置⽂件中引⼊component-scan(前后置通知:<context:component-scan base-package="ample.aspectAspectJNoArgs"/> 环绕通知:<context:component-scan base-package="ample.aspectAround"/>)
3、切⾯是观众(Audience),切点是节⽬表演(Performance.perform())
前置通知:在节⽬表演之前,观众就坐(调⽤Audience的takeSeats⽅法),并关掉⼿机(调⽤Audience的turnOffCellPhones⽅法);
后置通知:在节⽬表演结束,观众⿎掌(调⽤Audience的applaud⽅法);
异常通知:节⽬表演出现异常,观众要求退票(调⽤Audience的demandRefund⽅法);
环绕通知:其他与上⾯相同,只是在节⽬表演开始与结束时打印时间,统计节⽬表演时长;
4、通过执⾏Juggler的perform⽅法,从⽽执⾏切⾯Audience中相应的⽅法,达到通知的效果;
应⽤实例:观众观看表演所做出的相应⾏为
先列出相关接⼝以及类代码
节⽬表演接⼝(切点⽅法)
1 package ample.aspectAround;
2
3 /**
4  * Created by weixw on 2017/11/16.
5  */
6 public interface Performer {
7
8    void perform();
9 }
切点类实现接⼝Juggler
1 package ample.aspectAround;
2
3 import org.springframework.stereotype.Component;
4
5 /**
6  * Created by weixw on 2017/11/16.
7  */
8 @Component
9 public class Juggler implements Performer {
10    private int beanBags = 3;
11    public Juggler(){
12
13    }
14    public Juggler(int beanBags){
15        this.beanBags = beanBags ;
16    }
17    @Override
18    public void perform()  {
19        System.out.println("JUGGLING "+ beanBags + " BEANBAGS");
20        try {
21            Thread.sleep(1);
22        }catch (InterruptedException e){
23            e.printStackTrace();
24        }
25    }
26
27
28 }
上述代码都能共⽤,下⾯分别列举前后置通知与环绕通知区别代码
前后置通知(通过AspectJ注解实现,注意:<aop:aspectj-autoproxy/>不能少,它实现了切⾯相关⽅法绑定在切点上,切点⽅法执⾏就能触发相应通知)
XML配置⽂件:l(放在spring⽂件夹下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd  www.springframe    <!--使⽤前置通知和后置通知唯⼀⽅式:在前置通知中记录开始时间,并在后置通知中报告表演耗费的时长,必须保存开始时间。因为Audience是单例,如果像这样保    <!--存状态,会存在线程安全问题;-->
<context:component-scan base-package="ample.aspectAspectJNoArgs"/>
<aop:aspectj-autoproxy/>
</beans>
前后置通知切⾯实现类
package ample.aspectAspectJNoArgs;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* Created by weixw on 2017/11/16.
* 通过AspectJ注解实现切⾯编程
* 切点⽅法 id 默认是所依赖⽅法(public void performance(){})的⼩写⽅法名performance
*/
@Component
@Aspect
public class Audience {
@Pointcut("execution(* ample.aspectAspectJNoArgs.Performer.perform(..))") //定义切点
public void performance(){}
@Before("performance()")//表演之前
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
@Before("performance()")//表演之前
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones.");
}
@AfterReturning("performance()")//表演之后
public void applaud(){
System.out.println("CLAP CLAP CLAP CLAP CLAP ");
}
@AfterThrowing("performance()") //表演失败之后
public void demandRefund(){
System.out.println("Boo! We want our money back!");
}
}
环绕通知
XML配置⽂件:l(放在spring⽂件夹下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd  www.springframe    <!--前置通知和后置通知是在⼀个⽅法中实现,所以不需要保存变量值,⾃然是线程安全的;-->
<context:component-scan base-package="ample.aspectAround"/>
<!--通过component-scan⾃动扫描,@Component注解将Magician注册到spring容器-->
<aop:config>
<!--audience :切⾯  watchPerformance:切⾯⽅法  performance:切点-->
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* ample.aspectAround.Performer.perform(..))"/>                <aop:around pointcut-ref="performance" method="watchPerformance" />
</aop:aspect>
</aop:config>
</beans>
环绕通知切⾯实现类
package ample.aspectAround;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* Created by weixw on 2017/11/16.
*/
@Component
public class Audience {
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones.");
}
spring aop应用场景public void applaud(){
System.out.println("CLAP CLAP CLAP CLAP CLAP");
}
public void demandRefund(){
System.out.println("Boo! We want our money back!");
}
public void watchPerformance(ProceedingJoinPoint joinPoint){
try{
takeSeats(); //表演之前
turnOffCellPhones(); //表演之前
long start = System.currentTimeMillis();
System.out.println("The performance start ......");//节⽬开始
joinPoint.proceed(); //执⾏被通知的⽅法
System.out.println("The performance end ......");//节⽬结束
long end = System.currentTimeMillis(); //表演之后
applaud();//表演之后
System.out.println("The performance took milliseconds:"+ (end - start) );//表演时长
}catch (Throwable t){
demandRefund(); //表演失败之后
}
}
}
测试代码
环绕通知测试代码如下,前后置通知测试代码只需将配置⽂件名称改成l即可
1 package ample.aspectAround;/**
2  * Created by weixw on 2017/11/16.
3  */
4
5 import javafx.application.Application;
6 import javafx.stage.Stage;
7 import t.ApplicationContext;
8 import t.support.ClassPathXmlApplicationContext;
9
10 public class Driver extends Application {
11
12    public static void main(String[] args) {
13        launch(args);
14    }
15
16    @Override
17    public void start(Stage primaryStage) {
18        try {
19
20
21            ApplicationContext ctx = new ClassPathXmlApplicationContext("l");
22            Performer performer = (Performer) Bean("juggler");
23            performer.perform();
24
25        }catch (Exception e){
26            e.printStackTrace();
27        }
28    }
29 }
运⾏结果
环绕通知结果:
前后置通知结果:
总结
上述列出前后置通知和环绕通知样例。对于有变量缓存需求,线程安全的应⽤场景,前后置通知实现⽐较困难,⽽环绕通知实现就⾮常容易;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。