|
| 1 | +package com.springinaction.springidol; |
| 2 | + |
| 3 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 4 | +import org.aspectj.lang.annotation.*; |
| 5 | +import org.springframework.stereotype.Component; |
| 6 | + |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by zhangbin on 16/3/19. |
| 11 | + */ |
| 12 | +@Aspect |
| 13 | +@Component |
| 14 | +public class Audience1 { |
| 15 | + @Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))") |
| 16 | + public void performance() { |
| 17 | + } |
| 18 | + |
| 19 | +// @Before("performance()") |
| 20 | +// public void takeSeats() { |
| 21 | +// System.out.println("The audience is taking their seats."); |
| 22 | +// } |
| 23 | +// |
| 24 | +// @Before("performance()") |
| 25 | +// public void turnOffCellPhones() { |
| 26 | +// System.out.println("The audience is turning of their cellphones"); |
| 27 | +// } |
| 28 | +// |
| 29 | +// @After("performance()") |
| 30 | +// public void applaud() { |
| 31 | +// System.out.println("CLAP CLAP CLAP CLAP CLAP"); |
| 32 | +// } |
| 33 | +// |
| 34 | +// @AfterThrowing("performance()") |
| 35 | +// public void demandRefund() { |
| 36 | +// System.out.println("Boo! We want our money back!"); |
| 37 | +// } |
| 38 | + |
| 39 | + @Around("performance()") |
| 40 | + public void watchPerformance(ProceedingJoinPoint joinPoint) { |
| 41 | + try { |
| 42 | + System.out.println("The audience is taking their seats."); |
| 43 | + System.out.println("The audience is turning off their cellphones."); |
| 44 | + long start = System.currentTimeMillis(); |
| 45 | + joinPoint.proceed(); |
| 46 | + TimeUnit.MILLISECONDS.sleep(10); |
| 47 | + long end = System.currentTimeMillis(); |
| 48 | + System.out.println("CLAP CLAP CLAP CLAP CLAP"); |
| 49 | + System.out.println("The performance took " + (end - start) + " milliseconds."); |
| 50 | + } catch (Throwable t) { |
| 51 | + System.out.println("Boo! We want out money back!"); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments