笔记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 ...
随机推荐
- Python之旅.第三章.函数3.28
一.命名关键字参数: 什么是命名关键字参数?格式:在*后面参数都是命名关键字参数特点:1 必须被传值1 约束函数的调用者必须按照key=value的形式传值2 约束函数的调用者必须用我们指定的key名 ...
- 3-51单片机WIFI学习(开发板8266底层源码介绍)
上一篇链接 http://www.cnblogs.com/yangfengwu/p/8743502.html 直接上源码:注意源码有两部分,第一部分是一开始的时候写在模块内部的,另一部分是存在手机内 ...
- Python内置函数(33)——any
英文文档: any(iterable) Return True if any element of the iterable is true. If the iterable is empty, re ...
- Redux应用单一的store原则案例详解
在开发reac单页面应用的时候,页面的展示逻辑跟数据状态的关系管理变得越来越复杂,redux很好的解决这个问题.废话不多说,直接先上官网api链接. http://cn.redux.js.org/in ...
- 使用 slf4j抽象日志层 和 其他日志实现对接
前言 如果你正在提供一个开源的Java-jar,那么让你的项目仅依赖slf4j-api然后让你的用户在他们开发和运营环境中选择任意的日志实现绝对是个好想法,.作为终端用户,他们可以快速地从上面提到的日 ...
- SpringMVC架构的项目,js,css等静态文件导入有问题
发生原因 <servlet> <servlet-name>springmvc-mybaits</servlet-name> <servlet-class> ...
- Python基础数据类型之列表和元组
一.列表 list 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li ...
- intelj idea中JRebel激活
1.下载激活软件 https://github.com/ilanyu/ReverseProxy/releases/tag/v1.0 我这边下载的是: 2.双击运行 3.idea中, 不出意外,应该就激 ...
- 在删除一个指针之后,一定将该指针设置成空指针(即在delete *p之后一定要加上: p=NULL)
在删除一个指针之后,一定将该指针设置成空指针(即在delete *p之后一定要加上: p=NULL)
- SecureCRT安装
第一步:下载SecureCRT&SecureCRT激活工具 首先下载SecureCRT安装包和SecureCRT激活工具,SecureCRT&SecureCRT激活工具下载地址:链接: ...