您好,登錄后才能下訂單哦!
小編給大家分享一下spring切面如何使用傳遞給被通知方法的參數(shù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
場景:
BlankDisc代表CD實(shí)體,可以通過playTrack()方法直接播放某一個磁道中的歌曲。
需求是記錄每個磁道被播放的次數(shù)。
一種方法就是修改playTrack()方法,直接在每次調(diào)用的時候記錄這個數(shù)量。但是,記錄磁道的播放次數(shù)與播放本身是不同的關(guān)注點(diǎn),因此不應(yīng)該屬于playTrack()方法。這應(yīng)該是切面要完成的任務(wù)。
CompactDisc接口
public interface CompactDisc { //播放某一個磁道中的歌曲 void playTrack(String track); }
實(shí)現(xiàn)類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
/** * 切面類的任務(wù):記錄每個磁道被播放的次數(shù) * 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) { } //在播放前,為該磁道計數(shù) @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")); } }
測試結(jié)果 3,2,1,0
以上是“spring切面如何使用傳遞給被通知方法的參數(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。