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

一、注解@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" />
}

2.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:aop="http://www.springframework.org/schema/aop"
 5  xsi:schemaLocation="http://www.springframework.org/schema/beans
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/aop
 8      http://www.springframework.org/schema/aop/spring-aop.xsd">
 9
10   <bean id="eddie"
11       class="com.springinaction.springidol.Instrumentalist">
12     <property name="instrument">
13       <bean class="com.springinaction.springidol.Guitar" />
14     </property>
15   </bean>
16
17   <bean class="com.springinaction.springidol.AroundAudience" />
18
19     <aop:aspectj-autoproxy />
20
21 </beans>

3.或用java配置文件

 1 package com.springinaction.springidol;
 2
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 7
 8 @Configuration
 9 @EnableAspectJAutoProxy
10 @ComponentScan
11 public class Config{
12     @Bean
13     public AroundAudience aroundAudience() {
14         return new AroundAudience();
15     }
16
17     @Bean
18     public Performer eddie() {
19         Instrumentalist instrumentalist = new Instrumentalist();
20         instrumentalist.setInstrument(instrument());
21         return instrumentalist;
22     }
23
24     @Bean
25     public Instrument instrument() {
26         return new Guitar();
27     }
28 }

4.测试

 1 package com.springinaction.springidol;
 2
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9
10 @RunWith(SpringJUnit4ClassRunner.class)
11 //@ContextConfiguration("spring-aroundAdvice.xml")
12 @ContextConfiguration(classes=Config.class)//指定配置文件
13 public class AroundAdviceTest {
14   @Autowired
15   ApplicationContext context;
16
17   @Test
18   public void audienceShouldApplaud() throws Exception {
19     Performer eddie = (Performer) context.getBean("eddie");
20     eddie.perform();
21   }
22
23 }

结果:

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.

 1 package com.springinaction.springidol;
 2
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4
 5 public class AroundAudience {
 6   //<start id="audience_around_bean"/>
 7   public void watchPerformance(ProceedingJoinPoint joinpoint) {
 8     try {
 9       System.out.println("The audience is taking their seats.");
10       System.out.println("The audience is turning off their cellphones");
11       long start = System.currentTimeMillis(); //<co id="co_beforeProceed"/>
12
13       joinpoint.proceed(); //<co id="co_proceed"/>
14
15       long end = System.currentTimeMillis(); // <co id="co_afterProceed"/>
16       System.out.println("CLAP CLAP CLAP CLAP CLAP");
17       System.out.println("The performance took " + (end - start)
18           + " milliseconds.");
19     } catch (Throwable t) {
20       System.out.println("Boo! We want our money back!"); //<co id="co_afterException"/>
21     }
22   }
23   //<end id="audience_around_bean"/>
24 }

2.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:aop="http://www.springframework.org/schema/aop"
 5  xsi:schemaLocation="http://www.springframework.org/schema/beans
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/aop
 8      http://www.springframework.org/schema/aop/spring-aop.xsd">
 9
10   <bean id="eddie"
11       class="com.springinaction.springidol.Instrumentalist">
12     <property name="instrument">
13       <bean class="com.springinaction.springidol.Guitar" />
14     </property>
15   </bean>
16
17   <!-- <start id="audience_bean" /> -->
18   <bean id="audience"
19       class="com.springinaction.springidol.AroundAudience" />
20   <!-- <end id="audience_bean" /> -->
21
22   <!-- <start id="audience_aspect" /> -->
23   <aop:config>
24     <aop:aspect ref="audience">
25       <aop:pointcut id="performance" expression=
26           "execution(* com.springinaction.springidol.Performer.perform(..))"
27           />
28
29       <aop:around
30           pointcut-ref="performance"
31           method="watchPerformance" />
32     </aop:aspect>
33   </aop:config>
34   <!-- <end id="audience_aspect" /> -->
35
36 </beans>
时间: 02-29

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

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

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

一. 1.配置文件为xml时则切面类不用写aop的anotation 1 package com.springinaction.springidol; 2 3 public class Magician implements MindReader { 4 private String thoughts; 5 6 public void interceptThoughts(String thoughts) { 7 System.out.println("Intercepting volunteer

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

一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. 1 package concert; 2 3 public class CriticismEngineImpl implements CriticismEngine { 4 public CriticismEngineImpl() {} 5 6 // injected 7 private String[] cri

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml

一. 1.其他类和上个例子一样,观众类比使用@AspectJ方式的少了标签及pointcut,before等定义 1 package com.springinaction.springidol; 2 3 public class Audience { 4 public void takeSeats() { //<co id="co_takeSeats"/> 5 System.out.println("The audience is taking their sea

SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP

一. Aspect就是把会在应用中的不同地方重复出现的非业务功能的模块化,比如日志.事务.安全.缓存 In software development, functions that span multiple points of an application arecalled cross-cutting concerns. Typically, these cross-cutting concerns are conceptuallyseparate from (but often embed

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用&lt;s:message&gt;和ReloadableResourceBundleMessageSource实现国际化

一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application. 1 package spi

SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView

一.Spring支持的View Resolver 二.InternalResourceViewResolver Spring supports JSP views in two ways:? InternalResourceViewResolver ? Spring provides two JSP tag libraries, one for form-to-model binding and one providing general utility features. 1.在java中定义

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、&lt;jpa:repositories&gt;)

一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's Lonely Hearts Club Band", "The Beatles"); } < bean id = "sgtPeppers" class = "soundsystem.BlankDisc" c: _title = &quo