笔记11 在XML中声明切面(2)
为通知传递参数
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.结果

笔记11 在XML中声明切面(2)的更多相关文章
- 笔记10 在XML中声明切面(1)
1.无注解的Audience package XMLconcert; public class Audience { public void silenceCellPhones() { System. ...
- Spring AOP 在XML中声明切面
转载地址:http://www.jianshu.com/p/43a0bc21805f 在XML中将一个Java类配置成一个切面: AOP元素 用途 <aop:advisor> 定义AOP通 ...
- Spring 在XML中声明切面/AOP
在Spring的AOP配置命名空间中,我们能够找到声明式切面选择.看以下: <aop:config> <!-- AOP定义開始 --> <aop:pointcut/> ...
- 获取在attr.xml中声明的主题样式
在换肤时,先在attr.xml中定义Resource的属性名,再到stytle中,根据不同的主题,给属性赋值. 在布局文件中可直接以 android:background="?attr/a ...
- SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
游历SpringMVC源代码后发现,在web.xml中注冊的ContextLoaderListener监听器不过初始化了一个根上下文,只完毕了组件扫描和与容器初始化相关的一些工作,并没有探測到详细每一 ...
- SpringMVC: web.xml中声明DispatcherServlet时一定要添加load-on-startup标签
游历SpringMVC源码后发现,在web.xml中注册的ContextLoaderListener监听器只是初始化了一个根上下文,仅仅完成了组件扫描和与容器初始化相关的一些工作,并没有探测到具体每个 ...
- tornado学习笔记11 Web应用中模板(Template)使用应用实践
上一篇中(Web应用中模板的工作流程分析),已经分析了模板的渲染流程,以及相关参数获取及设置原理.这篇主要讲述模板在实际应用案例. 11.1 需求 根据用户输入的两次密码,判断两次密码是否一致,并将判 ...
- C#学习笔记11:C#中的顺序结构、分支结构、循环结构
顺序结构: 代码从Main()函数开始运行,从上到下,一行一行的执行,不漏掉代码. Int a=6; int b=5; int c=a+b; Console.Write(c); 分支结构: 代码有可能 ...
- maven使用笔记--在父pom中声明过的jar可以被继承,使子项目不用写版本号由父pom控制
将dependencies放到dependencyManagement中,如下: [html] view plaincopy <dependencyManagement> <depe ...
随机推荐
- zookeeper入门系列:概述
zookeeper可谓是目前使用最广泛的分布式组件了.其功能和职责单一,但却非常重要. 在现今这个年代,介绍zookeeper的书和文章可谓多如牛毛,本人不才,试图通过自己的理解来介绍zookeepe ...
- Python之旅.第三章.函数3.29
一.无参装饰器 1 开放封闭原则 软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案: 能够在不修改一个功能源代码以及调用方式的前提 ...
- 静态链表C语言数据结构
静态链表就是将数组实现单链表: int Malloc_SLL(StaticLinkList space) { int i = space[0].cur;//取得第一个头节点的下标 if( space[ ...
- 解决yii2中 Class yii/web/JsonParser does not exist, ReflectionException问题
最近在调试RESTful API示例时,出现以下错误: { "name": "Exception", "message": "Cl ...
- 泛型的 typeof
static void Main(string[] args) { TestTypeOf<string>(); Console.ReadKey(); } static void TestT ...
- 新概念英语(1-101)A Card From Jimmy
Lesson 101 A card from Jimmy 吉米的明信片 Listen to the tape then answer this question. Does Grandmother s ...
- 英语词汇(2)fall down,fall off和fall over
一.fall down,fall off和fall over都表示"摔倒.跌倒"的意思,但它们各自的含义不同. 1.fall over 落在...之上, 脸朝下跌倒 fall ov ...
- 关于terraform的状态管理
我们想在aws创建3台主机,使用ansible和terraform都是可以实现的. 用ansible可能是这样子的: - ec2: count: 10 image: ami-40d281120 ins ...
- C# Post提交数据
/// <summary> /// Post提交数据 /// </summary> /// <param name="postUrl">URL& ...
- [Micropython]TPYBoard v10x拼插编程实验 点亮心形点阵
一.什么是TPYBoard开发板 TPYBoard是以遵照MIT许可的MicroPython为基础,由TurnipSmart公司制作的一款MicroPython开发板,它基于STM32F405单片机, ...