spring: 使用Aspectj代理EnabelAspectjAutoProxy
使用JavaConfig的话,可以在配置类的类级别上通过使用EnableAspectJ-AutoProxy注解启用自动代理功能。
package ch2.test; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Audience { //定义命名的切点 @Pointcut("execution(** ch2.test.Performance.perform(..))") public void Performance(){} //表演开始之前:关闭手机 @Before("Performance()") public void silenCellPhones() { System.out.println("silen cell phones"); } //表演开始之前:入座 @Before("Performance()") public void takingSeats() { System.out.println("take seats"); } //表演开始之后:鼓掌 @AfterReturning("Performance()") public void applause() { System.out.println("clap clap clap"); } //表演结束之后:表演失败退票 @AfterThrowing("Performance()") public void demandRefund() { System.out.println("demand refund"); } }
config
package ch2.test; 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 @ComponentScan //声明自动代理: 开启自动代理 @EnableAspectJAutoProxy public class ConcertConfig { @Bean public Audience audience() { return new Audience(); } }