详解spring切面使用传递给被通知方法的参数
程序员文章站
2023-12-04 11:54:10
本文介绍了详解spring切面使用传递给被通知方法的参数,分享给大家,具体如下:
场景:
blankdisc代表cd实体,可以通过playtrack()方法直接播放...
本文介绍了详解spring切面使用传递给被通知方法的参数,分享给大家,具体如下:
场景:
blankdisc代表cd实体,可以通过playtrack()方法直接播放某一个磁道中的歌曲。
需求是记录每个磁道被播放的次数。
一种方法就是修改playtrack()方法,直接在每次调用的时候记录这个数量。但是,记录磁道的播放次数与播放本身是不同的关注点,因此不应该属于playtrack()方法。这应该是切面要完成的任务。
compactdisc接口
public interface compactdisc { //播放某一个磁道中的歌曲 void playtrack(string track); }
实现类blankdisc
public class blankdisc implements compactdisc { private string title; private string artist; private list<string> tracks; public void settitle(string title) { this.title = title; } public void setartist(string artist) { this.artist = artist; } public void settracks(list<string> tracks) { this.tracks = tracks; } @override public void playtrack(string track) { system.out.println("-track: " + track); } }
切面类tracecount
/** * 切面类的任务:记录每个磁道被播放的次数 * created by administrator on 2017/12/1. */ @component @aspect public class trackcounter { private map<string, integer> trackcounts = new hashmap<>(); @pointcut("execution(* chapter04.aop_args.blankdisc.playtrack(string)) && args(track)") public void trackplayed(string track) { } //在播放前,为该磁道计数 @before("trackplayed(track)") public void counttrack(string track) { int currentcount = getplaycount(track); trackcounts.put(track, currentcount + 1); } public int getplaycount(string track) { return trackcounts.containskey(track) ? trackcounts.get(track) : 0; } }
applicationcontext.xml配置文件
<context:component-scan base-package="aop_test,chapter04"/> <bean id="compactdisc" class="chapter04.aop_args.blankdisc"> <property name="title" value="sgt. pepper's lonely hearts club band" /> <property name="artist" value="the beatles" /> <property name="tracks"> <list> <value>sgt. pepper's lonely hearts club band</value> <value>with a little help from my friends</value> <value>lucy in the sky with diamonds</value> <value>getting better</value> <value>fixing a hole</value> <value>she's leaving home</value> <value>being for the benefit of mr. kite!</value> <value>within you without you</value> <value>when i'm sixty-four</value> <value>lovely rita</value> <value>good morning good morning</value> <value>sgt. pepper's lonely hearts club band (reprise)</value> <value>a day in the life</value> </list> </property> </bean> <!-- 开启aop注解 --> <aop:aspectj-autoproxy/>
测试
@runwith(springjunit4classrunner.class) @contextconfiguration("classpath:applicationcontext.xml") public class test_aop_args { @autowired compactdisc cd; @autowired trackcounter trackcounter; @test public void test(){ cd.playtrack("sgt. pepper's lonely hearts club band"); cd.playtrack("with a little help from my friends"); cd.playtrack("lucy in the sky with diamonds"); cd.playtrack("sgt. pepper's lonely hearts club band"); cd.playtrack("with a little help from my friends"); cd.playtrack("sgt. pepper's lonely hearts club band"); system.out.println(trackcounter.getplaycount("sgt. pepper's lonely hearts club band")); system.out.println(trackcounter.getplaycount("with a little help from my friends")); system.out.println(trackcounter.getplaycount("lucy in the sky with diamonds")); system.out.println(trackcounter.getplaycount("getting better")); } }
测试结果 3,2,1,0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Spring之WEB模块配置详解
下一篇: Java定时器Timer使用方法详解