您好,登錄后才能下訂單哦!
小編給大家分享一下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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。