一、注解@AspectJ形式

1.

package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class AroundAudience { @Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))")
public void performance() {
} //<start id="audience_around_bean" />
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("The audience is taking their seats.");
System.out.println("The audience is turning off their cellphones"); long start = System.currentTimeMillis();
joinpoint.proceed();
long end = System.currentTimeMillis(); System.out.println("CLAP CLAP CLAP CLAP CLAP"); System.out.println("The performance took " + (end - start)
+ " milliseconds.");
} catch (Throwable t) {
System.out.println("Boo! We want our money back!");
}
}
//<end id="audience_around_bean" />
}

The advice method will do everything it needs to do; and when it’s ready to pass control to the advised method, it will call ProceedingJoinPoint ’s proceed() method.

What’s also interesting is that just as you can omit a call to the proceed() method to block access to the advised method, you can also invoke it multiple times from within the advice. One reason for doing this may be to implement retry logic to perform repeated attempts on the advised method should it fail.

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> <bean class="com.springinaction.springidol.AroundAudience" /> <aop:aspectj-autoproxy /> </beans>

3.或用java配置文件

 package com.springinaction.springidol;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class Config{
@Bean
public AroundAudience aroundAudience() {
return new AroundAudience();
} @Bean
public Performer eddie() {
Instrumentalist instrumentalist = new Instrumentalist();
instrumentalist.setInstrument(instrument());
return instrumentalist;
} @Bean
public Instrument instrument() {
return new Guitar();
}
}

4.测试

 package com.springinaction.springidol;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("spring-aroundAdvice.xml")
@ContextConfiguration(classes=Config.class)//指定配置文件
public class AroundAdviceTest {
@Autowired
ApplicationContext context; @Test
public void audienceShouldApplaud() throws Exception {
Performer eddie = (Performer) context.getBean("eddie");
eddie.perform();
} }

结果:

The audience is taking their seats.
The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
The performance took 0 milliseconds.

二、xml形式<aop:config></aop:config>

1.

 package com.springinaction.springidol;

 import org.aspectj.lang.ProceedingJoinPoint;

 public class AroundAudience {
//<start id="audience_around_bean"/>
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("The audience is taking their seats.");
System.out.println("The audience is turning off their cellphones");
long start = System.currentTimeMillis(); //<co id="co_beforeProceed"/> joinpoint.proceed(); //<co id="co_proceed"/> long end = System.currentTimeMillis(); // <co id="co_afterProceed"/>
System.out.println("CLAP CLAP CLAP CLAP CLAP");
System.out.println("The performance took " + (end - start)
+ " milliseconds.");
} catch (Throwable t) {
System.out.println("Boo! We want our money back!"); //<co id="co_afterException"/>
}
}
//<end id="audience_around_bean"/>
}

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.AroundAudience" />
<!-- <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(..))"
/> <aop:around
pointcut-ref="performance"
method="watchPerformance" />
</aop:aspect>
</aop:config>
<!-- <end id="audience_aspect" /> --> </beans>

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-007-定义切面的around advice的更多相关文章

  1. 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 ...

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

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

  3. 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 ...

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

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

  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-012-AOP总结

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

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

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

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

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

  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. 关于.NET编译的目标平台(AnyCPU,x86,x64)

    转载:http://blog.sina.com.cn/s/blog_78b94aa301014i8r.html 今天有项目的代码收到客户的反馈,要求所有的EXE工程的目标平台全部指定成x86,而所有D ...

  2. Java的内存泄漏_与C/C++对比(转载总结)

    原文网址:http://developer.51cto.com/art/201111/302465.htm Java内存泄露的理解与解决(1)   一般来说内存泄漏有两种情况.一种情况如在C/C++ ...

  3. ubuntu tab命令补全失效

    主要是由于环境变量设置出了问题,修改/etc/environment即可. sudo nano /etc/environment 修改后source /etc/environment

  4. 第一篇、Swift_Textkit的基本使用

    简介: iOS7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit(文本工具箱).TextKit 由许多新的 UIKit 类组成,顾名思义,这些类就是用来处理文本的. 1.NSTex ...

  5. Objective-C 学习笔记(Day 2)

    ------------------------------------------- 如何根据题目准确完整清晰的声明一个类并实现给定的行为 /*  //下面这个程序教大家如何根据题目去声明一个类,并 ...

  6. JavaScript解析json

    后台数据经常以json数据格式传回前台,解析当然首选JSON对象. JSON对象有两个方法,使用JSON.parse(str)可以将json字符串解析成js中的对象. var o = JSON.par ...

  7. 封装DB类

    封装DB类     一般一个类单独书写在一个Php文件中,为了见名知意,会对文件名有一个规范:类名.class.php 第1步:     创建DB类 第2 步:     属性设计 第3步:     初 ...

  8. CentOS7 安装LAMP环境

    1.使用yum安装 yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-pos ...

  9. remastersys修改默认选项

    1.vim /etc/remastersys/isolinux/isolinux.cfg.vesamenu default vesamenu.c32prompt 0timeout 100 menu t ...

  10. PHP5.3后在本机运行很慢的解决方法

    方法一:这是因为PHP 5.3在面对数据库配置信息中的“localhost”会犹豫,因此直接把这个地址改名为“127.0.0.1”,这个IP是IPv4下面的本地网络地址,实际作用和“localhost ...