场景:
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