为通知传递参数

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)的更多相关文章

  1. 笔记10 在XML中声明切面(1)

    1.无注解的Audience package XMLconcert; public class Audience { public void silenceCellPhones() { System. ...

  2. Spring AOP 在XML中声明切面

    转载地址:http://www.jianshu.com/p/43a0bc21805f 在XML中将一个Java类配置成一个切面: AOP元素 用途 <aop:advisor> 定义AOP通 ...

  3. Spring 在XML中声明切面/AOP

    在Spring的AOP配置命名空间中,我们能够找到声明式切面选择.看以下: <aop:config> <!-- AOP定义開始 --> <aop:pointcut/> ...

  4. 获取在attr.xml中声明的主题样式

    在换肤时,先在attr.xml中定义Resource的属性名,再到stytle中,根据不同的主题,给属性赋值. 在布局文件中可直接以  android:background="?attr/a ...

  5. SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签

    游历SpringMVC源代码后发现,在web.xml中注冊的ContextLoaderListener监听器不过初始化了一个根上下文,只完毕了组件扫描和与容器初始化相关的一些工作,并没有探測到详细每一 ...

  6. SpringMVC: web.xml中声明DispatcherServlet时一定要添加load-on-startup标签

    游历SpringMVC源码后发现,在web.xml中注册的ContextLoaderListener监听器只是初始化了一个根上下文,仅仅完成了组件扫描和与容器初始化相关的一些工作,并没有探测到具体每个 ...

  7. tornado学习笔记11 Web应用中模板(Template)使用应用实践

    上一篇中(Web应用中模板的工作流程分析),已经分析了模板的渲染流程,以及相关参数获取及设置原理.这篇主要讲述模板在实际应用案例. 11.1 需求 根据用户输入的两次密码,判断两次密码是否一致,并将判 ...

  8. C#学习笔记11:C#中的顺序结构、分支结构、循环结构

    顺序结构: 代码从Main()函数开始运行,从上到下,一行一行的执行,不漏掉代码. Int a=6; int b=5; int c=a+b; Console.Write(c); 分支结构: 代码有可能 ...

  9. maven使用笔记--在父pom中声明过的jar可以被继承,使子项目不用写版本号由父pom控制

    将dependencies放到dependencyManagement中,如下: [html] view plaincopy <dependencyManagement> <depe ...

随机推荐

  1. 从同步阻塞聊到Java三种IO方式

    本文总结自 https://zhuanlan.zhihu.com/p/34408883, https://www.zhihu.com/question/19732473中愚抄的回答, http://b ...

  2. LR之error(一)

    1 录制时频繁卡死的解决方案 添加数据保护 路径:计算机--高级系统设置(环境变量设置的上级窗口)--高级--设置--数据执行保护 更改LR录制设置,将run-time setting的brower改 ...

  3. Win7下安装composer, 并使用其安装smarty

    安装composer需要开启PHP openssl扩展. 1) 先查看PHP是否开启了openssl扩展 键盘win+r 输出cmd, 可以看到Dos窗口, 然后执行php -m (需要添加PHP环境 ...

  4. Jquery blokckUI 快速入门

    $("#btnSubmit").click(function() { $.blockUI({ message : $("#loginForm"), css : ...

  5. SSO的全方位解决方案 - Kerberos协议(RFC 1510)

    一.桌面SSO和WEB-SSO的局限性 前面我们的解决方案(桌面SSO和WEB-SSO)都有一个共性:要想将一个应用集成到我们的SSO解决方案中,或多或少的需要修改应用程序. Web应用需要配置一个我 ...

  6. less初学手记

    less语言学习手记 工具下载 在less学习中,我们都会需要随时编译我们的less文件,查看生成的css样式表是否正确,以及是否符合我们的要求.推荐一款编译软件供大家下载使用:koala,本软件支持 ...

  7. kafka--- consumer 消费消息

    1. consumer API kafka 提供了两套 consumer API: 1. The high-level Consumer API 2. The SimpleConsumer API 其 ...

  8. SpringBoot使用log4j

    1.添加log4j相关依赖 在pom.xml文件中添加相关依赖: <!--配置log4j--> <dependency> <groupId>org.springfr ...

  9. java 中String类的常用方法总结,带你玩转String类。

    String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...

  10. Android学习——移植tr069程序到Android平台

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...