This article introduces the detailed explanation of the parameters passed to the notification method using spring sections. It is shared with you, as follows:
Scene:
BlankDisc represents a CD entity, and songs in a certain track can be played directly through the playTrack() method.
The requirement is to record the number of times each track is played.
One way is to modify the playTrack() method and record this number directly at each call. However, the number of playbacks of the recording track is different from the play itself, so it should not belong to the playTrack() method. This should be the task to be completed on the section.
CompactDisc interface
public interface CompactDisc { //Play the song void playTrack(String track); } Implement BlankDisc class
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
/** * Task of the section class: record the number of times each track is played* 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 configuration file
<context:component-scan base-package="aop_test,chapter04"/> <bean id="compactDisc" > <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>Whithin 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> <!-- Turn on aop annotation--> <aop:aspectj-autoproxy/>
test
@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")); } } Test results 3, 2, 1, 0
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.