Detail the spring section using the parameters passed to the notified method
- 2020-12-09 00:51:14
- OfStack
This article introduces the details of the spring section using the parameters passed to the notified method and shares them with you as follows:
Scene:
BlankDisc stands for CD entity and can play songs directly from one track via the playTrack() method.
The requirement is to record the number of times each track is played.
One way is to modify the playTrack() method to record the number directly at each call. However, recording the number of tracks played is a different concern than playing itself, so it should not be part of the playTrack() method. This should be the task of the section.
CompactDisc interface
public interface CompactDisc {
// Play a 1 A song on a track
void playTrack(String track);
}
The implementation class 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);
}
}
Cut class TraceCount
/**
* Section class task: 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 playing, count for the 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 profile
<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>
<!-- open aop annotations -->
<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