为通知传递参数
1.声明一个CompactDiscs接口。内部包含两个方法:
- show() 用于显示唱片的名字和艺术风格
- playTrack(int number) 根据传入的磁道数播放相应磁道的音乐(假设每个磁道就一首歌)
package XMLsoundsystem; public interface CompactDiscs {
void show(); void playTrack(int number);
}
2.实现接口的类BlankDisc
package XMLsoundsystem; import java.util.List; public class BlankDisc implements CompactDiscs {
private String title;
private String artist;
private List<String> tracks; 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;
} @Override
public void playTrack(int number) {
System.out.println(tracks.get(number - ));
} @Override
public void show() {
System.out.println("唱片的名字:" + title);
System.out.println("唱片属于的艺术流派:" + artist);
} }
3.无注解的TrackCounter,用来记录播放的次数。
package XMLsoundsystem; import java.util.HashMap;
import java.util.Map; public class TrackCounter {
private Map<Integer, Integer> trackCounts = new HashMap<Integer, Integer>(); public void countTrack(int trackNumber) {
int currentCount = getPlayCount(trackNumber);
trackCounts.put(trackNumber, currentCount + );
} public int getPlayCount(int trackNumber) {
return trackCounts.containsKey(trackNumber) ? trackCounts.get(trackNumber) : ;
}
}
4.XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="trackCounter" class="XMLsoundsystem.TrackCounter"></bean>
<bean id="cd" class="XMLsoundsystem.BlankDisc">
<property name="title" value="AAAAABBBBCCCC"></property>
<property name="artist" value="CCCCCBBBBBAAAA"></property>
<property name="tracks">
<list>
<value></value>
<value></value>
<value></value>
<value></value>
<value></value>
</list>
</property>
</bean>
<aop:config>
<aop:aspect ref="trackCounter">
<aop:pointcut expression="execution(* XMLsoundsystem.CompactDiscs.playTrack(int)) and args(trackNumber)" id="trackPlayed"/>
<aop:before pointcut-ref="trackPlayed" method="countTrack"/>
</aop:aspect>
</aop:config>
</beans>
5.测试
package XMLsoundsystem; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ConcertConfig4.xml")
public class TrackCounterTest {
@Autowired
private CompactDiscs cd;
@Autowired
private TrackCounter counter; @Test
public void test() {
cd.show();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
cd.playTrack();
for (int i = ; i < ; i++) {
System.out.println("磁道" + i + "播放了" + counter.getPlayCount(i) + "次");
}
}
}
6.结果
通过切面引入新的功能
1.定义表演接口Performance.java
package concert3; public interface Performance {
public void perform();
}
2.实现接口,Classcial.java
package concert3; public class Classcial implements Performance { @Override
public void perform() {
// TODO Auto-generated method stub
System.out.println("我是古典音乐会!");
} }
3.定义观众类,即切面 Audience.java
package concert3; import org.aspectj.lang.ProceedingJoinPoint; public class Audience { // ProceedingJoinPoint作为参数。
// 这个对象是必须要有的,因为 你要在通知中通过它来调用被通知的方法。
// 通知方法中可以做任何的 事情,当要将控制权交给被通知的方法时,它需要调 用ProceedingJoinPoint的proceed()方法。
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phone");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
}
4.定义新功能接口 Encoreable.java
package concert3; public interface Encoreable {
void performEncore();
}
5.实现新接口DefaultEncoreable.java
package concert3; public class DefaultEncoreable implements Encoreable { @Override
public void performEncore() {
// TODO Auto-generated method stub
System.out.println("川剧变脸");
} }
6.XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="concert3.Classcial"></bean>
<bean id="audience" class="concert3.Audience"></bean>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* concert3.Performance.perform(..))" id="performance"/>
<aop:around method="watchPerformance" pointcut-ref="performance"/>
<aop:declare-parents types-matching="concert3.Performance+"
implement-interface="concert3.Encoreable"
default-impl="concert3.DefaultEncoreable"/>
</aop:aspect>
</aop:config>
</beans>
7.测试
package concert3; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration(classes = concert3.ConcertConfig.class)
@ContextConfiguration("classpath:ConcertConfig5.xml")
public class ConcertTest {
@Autowired
public Performance p;
@Autowired
public Encoreable en; @Test
public void test() { p.perform();
System.out.println("-----------------------------");
System.out.println("自己创建对象调用");
en.performEncore();
System.out.println("-----------------------------");
System.out.println("通过Performance对象调用“新方法”");
Encoreable e = (Encoreable) p;
e.performEncore();
}
}
8.结果