一、

you can also define pointcuts that can be used across multiple aspects by placing the <aop:pointcut> elements within the scope of the <aop:config> element.

1.其他类和上个例子一样,观众类比使用@AspectJ方式的少了@AspectJ标签及pointcut,before、after等定义

 package com.springinaction.springidol;

 public class Audience {
public void takeSeats() { //<co id="co_takeSeats"/>
System.out.println("The audience is taking their seats.");
} public void turnOffCellPhones() { //<co id="co_turnOffCellPhones"/>
System.out.println("The audience is turning off their cellphones");
} public void applaud() { //<co id="co_applaud"/>
System.out.println("CLAP CLAP CLAP CLAP CLAP");
} public void demandRefund() { //<co id="co_demandRefund"/>
System.out.println("Boo! We want our money back!");
}
}

2.xml(由于直接在xml定义切面,所以不用写<AOP:ASPECTJ-AUTOPROXY>)

(1)

 <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="eddie"
class="com.springinaction.springidol.Instrumentalist">
<property name="instrument">
<bean class="com.springinaction.springidol.Guitar" />
</property>
</bean> <!--<start id="audience_bean" />-->
<bean id="audience"
class="com.springinaction.springidol.Audience" />
<!--<end id="audience_bean" />--> <!--<start id="audience_aspect" />-->
<aop:config>
<aop:aspect ref="audience"><!--<co id="co_refAudienceBean"/>--> <aop:before pointcut=
"execution(* com.springinaction.springidol.Performer.perform(..))"
method="takeSeats" /> <!--<co id="co_beforePointcut"/>--> <aop:before pointcut=
"execution(* com.springinaction.springidol.Performer.perform(..))"
method="turnOffCellPhones" /> <!--<co id="co_beforePointcut2"/>--> <aop:after-returning pointcut=
"execution(* com.springinaction.springidol.Performer.perform(..))"
method="applaud" /> <!--<co id="co_afterPointcut"/>--> <aop:after-throwing pointcut=
"execution(* com.springinaction.springidol.Performer.perform(..))"
method="demandRefund" /> <!--<co id="co_afterThrowingPointcut"/>--> </aop:aspect>
</aop:config>
<!--<end id="audience_aspect" />--> </beans>

织入的图解

(2)或

 <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="eddie"
class="com.springinaction.springidol.Instrumentalist">
<property name="instrument">
<bean class="com.springinaction.springidol.Guitar" />
</property>
</bean> <!--<start id="audience_bean" />-->
<bean id="audience"
class="com.springinaction.springidol.Audience" />
<!--<end id="audience_bean" />--> <!--<start id="audience_aspect" />-->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression=
"execution(* com.springinaction.springidol.Performer.perform(..))"
/> <!--<co id="co_defPointcut"/>--> <aop:before
pointcut-ref="performance"
method="takeSeats" /> <!--<co id="co_refPointcut"/>-->
<aop:before
pointcut-ref="performance"
method="turnOffCellPhones" /> <!--<co id="co_refPointcut"/>-->
<aop:after-returning
pointcut-ref="performance"
method="applaud" /> <!--<co id="co_refPointcut"/>-->
<aop:after-throwing
pointcut-ref="performance"
method="demandRefund" /> <!--<co id="co_refPointcut"/>-->
</aop:aspect>
</aop:config>
<!--<end id="audience_aspect" />--> <!--<start id="audience_around_advice" />-->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance2" expression=
"execution(* com.springinaction.springidol.Performer.perform(..))"
/> <aop:around
pointcut-ref="performance2"
method="watchPerformance()" /> <!--<co id="co_around"/>-->
</aop:aspect>
</aop:config>
<!--<end id="audience_around_advice" />-->
</beans>

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML

    一. 1.配置文件为xml时则切面类不用写aop的anotation package com.springinaction.springidol; public class Magician impl ...

  2. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法@DeclareParents、<aop:declare-parents>

    一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automat ...

  3. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-005-定义切面使用@Aspect、@EnableAspectJAutoProxy、<aop:aspectj-autoproxy>

    一. 假设有如下情况,有一个演凑者和一批观众,要实现在演凑者的演凑方法前织入观众的"坐下"."关手机方法",在演凑结束后,如果成功,则织入观众"鼓掌& ...

  4. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-003-Spring对AOP支持情况的介绍

    一. 不同的Aop框架在支持aspect何时.如何织入到目标中是不一样的.如AspectJ和Jboss支持在构造函数和field被修改时织入,但spring不支持,spring只支持一般method的 ...

  5. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut

    一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...

  6. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-002-AOP术语解析

    一. 1.Advice Advice是切面的要做的操作,它定义了what.when(什么时候要做什么事) aspects have a purpose—a job they’re meant to d ...

  7. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-012-AOP总结

    1.AOP是面向对象编程的有力补充,它可以让你把分散在应用中的公共辅助功能抽取成模块,以灵活配置,减少了重复代码,让类更关注于自身的功能

  8. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

    一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. package ...

  9. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2

    一. 情景:有个魔术师会读心术,常人一想一事物他就能读到.以魔术师为切面织入常人的内心. 二. 1. // <start id="mindreader_java" /> ...

  10. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-008-带参数的ADVICE

    一. 假设有情形如:cd里有很多轨,当播放音乐时,要统计每个音轨的播放次数,这些统计操作不应放在播放方法里,因为统计不是播放音乐的主要职责,这种情况适合应用AOP. 二. 1. package sou ...

随机推荐

  1. ios Trace xcode buile count

    前言: 1.记录xcode编辑次数很有必要,特别是在频繁发版本时和根据现有编译次数记录估算工期时间很有帮助 2.全部自动化处理,告别手动时代 正文: 1.新建工程或者现有工程里设置: 然后设置xcod ...

  2. .net LINQ and PLINQ

    本文  学习自  微软官网文档   2016/12 LINQ 背景   以前写与DB 相关的代码, 程序员须要懂开发语言(C#, VB)和查询语言跟数据库交互. LINQ 的出现使应用程序形成基于集合 ...

  3. JavaScript中关于创建对象的笔记

    1,最基本的两种创建对象的方式:构造函数|| 字面量 构造函数: var person = new Object(); person.name = "chen1zee1"; per ...

  4. 03_HttpClient_Post请求

    [实例1.最最最简洁的POST请求] @Test public void test1() throws Exception{ //1.创建Htpclient实例(可关闭 Closeable) Clos ...

  5. C++结构体对象数组的二进制方式读写

    以一个学生信息的结构体数组为例. #include<iostream>#include<string>#include<fstream>using namespac ...

  6. DLL详解及Denpendcy Walker的使用

    下面的文章被N次转载,为了尊重原作,\(^o^)/~,贴出最早发布这篇文章的地址及作者.   动态链接库 Windows的活动大陆 2006-07-26 09:21  作者:狂ρκ来源:电脑爱好者 在 ...

  7. 标准C++中string类的用法

    转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...

  8. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  9. centos 安装php ide (eclipse + php 插件)

    1.检查更新并安装eclipse  yum check-update  yum install eclipse*此时的 eclipse 已经安装好了,默认是在/usr/lib/下的,可以通过cd /u ...

  10. Android开发系列之搭建开发环境

    接触Android好久了,记得09年刚在中国大陆有点苗头的时候,我就知道了google有个Android,它是智能机操作系统.后来在Android出1.5版本之后,我第一时间下载了eclipse开发工 ...