一、

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. Java_Web _Servlet生命周期实验

    第一次加载这个servlet程序时(选择右边的servlet程序,注意servlet程序没有main函数,因此执行的是run as servlet Application ),同时执行init()方法 ...

  2. java星座、年龄、日期等

    星座: public static String getStar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(dat ...

  3. 在有大量数据时 少用In(数据会丢失) 用left join 代替

    select @From, @To, EffectiveDate, GETDATE(), Rate from Config_Currency_ExchangeRate_Temp where Effec ...

  4. Appium 解决中文输入问题

  5. 12天学好C语言——记录我的C语言学习之路(Day 11)

    12天学好C语言--记录我的C语言学习之路 Day 11: 因为指针部分比较的难,所以我们花费的时间也是最长的,希望大家耐的住性子,多多理解,多多打代码.好了,废话不多说,来看第11天的学习. //编 ...

  6. 05顺序队列_Queue--(栈与队列)

    #include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...

  7. [ADO.NET]写入数据

    关键字:.NET,SQL SERVER,SQLBULKCOPY 插入一条数据的操作(SqlCmd). public void InsertOneIntoSourceDB() { if (DBConne ...

  8. php header示例代码(推荐)

    <?php /*** Function: PHP header() examples (PHP) ** Desc: Some examples on how to use the header( ...

  9. Struts2文件上传方式与上传失败解决方式

    首先将几个对象弄出来第一个 上传页面第二个 上传action第三个 startut2配置文件 我的文字描述不是很好,但是终归是自己写出来的,后来我在网上看到一篇关于文件上传描述的非常清楚的文章, 链接 ...

  10. rpm的一些用法

    rpm2cpio *.rpm | cpio -imd     #解压一个rpm包 rpm -ivh *.rpm  --force         #强制安装这个rpm包 rpm -ivh *.rpm  ...