小编给大家分享一下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切面如何使用传递给被通知方法的参数”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。