package soundSystem;
//定义CD接口i
public interface CompactDisc {
void play();
void playTrack(int i);
}
定义空CD类,继承CD
package soundSystem;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class BlankDisc implements CompactDisc {
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public List<String> getTracks() {
return tracks;
}
public void setTracks(List<String> tracks) {
this.tracks = tracks;
}
private String title;
private String artist;
private List<String> tracks;
public BlankDisc() {
}
public BlankDisc(String artist, String title, List<String> tracks) {
this.artist = artist;
this.title = title;
this.tracks = tracks;
}
public void play() {
System.out.println("Playing " + title + " by " + artist);
for (String track: tracks) {
System.out.println("-Track: " + track);
}
}
public void playTrack(int num) {
System.out.println("-Track: " + tracks.get(num));
}
}
实现切面
package soundSystem;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import java.util.HashMap;
import java.util.Map;
@Aspect
public class TrackCounter {
private Map<Integer, Integer> trackCounts =
new HashMap<>();
@Pointcut(//定义切点
"execution(* soundSystem.CompactDisc.playTrack(int))"+
"&& args(trackNumber)")
public void trackPlayed(int trackNumber){}
@Before("trackPlayed(trackNumber)")//在切点前执行
public void countTrack(int trackNumber){
System.out.println(trackNumber);
int currentCount = getPlayCount(trackNumber);
trackCounts.put(trackNumber, currentCount + 1);
}
public int getPlayCount(int trackNumber) {
System.out.println(trackNumber);
return trackCounts.containsKey(trackNumber) ?
trackCounts.get(trackNumber) : 0;
}
}
配置切面,注入Bean
package soundSystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableAspectJAutoProxy
public class TrackCounterConfig {
@Bean
public CompactDisc sgtPeppers() {
BlankDisc cd = new BlankDisc();
cd.setTitle("Sgt. Pepper's Lonely Hearts Club Band");
cd.setArtist("The Beatles");
List<String> tracks = new ArrayList<String>();
tracks.add("Sgt. Pepper's Lonely Hearts Club Band");
tracks.add("With a Little Help from My Friends");
tracks.add("Lucky in the Sky with Diamonds");
tracks.add("Getting Better");
tracks.add("Fixing a Hole");
tracks.add("zzzz");
tracks.add("justinniu");
cd.setTracks(tracks);
return cd;
}
@Bean
public TrackCounter trackCounter() {
return new TrackCounter();
}
}
最后测试
package soundSystem;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCounterConfig.class)
public class TrackCounterTest {
@Autowired
private CompactDisc cd;
@Autowired
private TrackCounter counter;
@Test
public void testTrackCounter() {
cd.playTrack(0);
cd.playTrack(1);
cd.playTrack(2);
cd.playTrack(2);
cd.playTrack(2);
cd.playTrack(2);
cd.playTrack(6);
cd.playTrack(6);
assertEquals(1, counter.getPlayCount(0));
assertEquals(1, counter.getPlayCount(1));
assertEquals(4, counter.getPlayCount(2));
assertEquals(0, counter.getPlayCount(3));
assertEquals(0, counter.getPlayCount(4));
assertEquals(0, counter.getPlayCount(5));
assertEquals(2, counter.getPlayCount(6));
}
public void setCounter(TrackCounter counter) {
this.counter = counter;
}
public TrackCounter getCounter() {
return counter;
}
public void setCd(BlankDisc cd) {
this.cd = cd;
}
public CompactDisc getCd() {
return cd;
}
}
遇到的错误
1.Spring 切面没有运行成功,没有@Aspect,会出现cd.trackPlay()但是切面并没有执行,测试也会报错。
2. 如果已经@Aspect但报出如下的错误,说明没有配置好jar,需要aspectjrt-1.8.xx.jar 和aspectjweaver-1.8.xx.jar
java.lang.IllegalStateException: Failed to load ApplicationContext
3.@Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog();添加这个会把输出记录到日志里,所有调试输出测试也不可见