package com.advice;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/25 23:41
*/
public class Performer {
public void doPerform() {
System.out.println("Performer do perform ....................... ");
}
}
package com.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/25 23:42
*/
@Aspect//定义切面
public class Audience {
//定义切点
@Pointcut("execution(* com.advice.Performer.doPerform(..))")
public void doPerform(){}
@Before("doPerform()")
public void takeSeas() {
System.out.println("The audience is taking their seats.");
}
@Before("doPerform()")
public void turnOffPhone() {
System.out.println("The audience is turn off their cellphone.");
}
@AfterReturning("doPerform()")
public void applaund() {
System.out.println("CLAP CLAP CLAP CLAP ...");
}
@AfterThrowing("doPerform()")
public void demandRefund() {
System.out.println("Boo! we want our money back!");
}
@Around("doPerform()")
public void watchPerfomance(ProceedingJoinPoint joinPoint) {
try {
Long start = System.currentTimeMillis();
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("The performance took "+(end-start)+" milliseconds");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
<!-- aop 增强自动代理 --> <aop:aspectj-autoproxy/> <bean id="audience" class="com.advice.Audience"/> <bean id="performer" class="com.advice.Performer"/>
@Test
public void testDoPerform() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:smart-context.xml");
//代理为指向Interface的代理
Performer performer = (Performer) context.getBean("performer");
System.out.println("+++++++++++++++++++++++++++++++++");
performer.doPerform();
}
+++++++++++++++++++++++++++++++++ 2017-04-26 20:51:16,980 DEBUG [main] (AbstractBeanFactory.java:251) - Returning cached instance of singleton bean 'audience' The audience is taking their seats. The audience is turn off their cellphone. Performer do perform ....................... The performance took 91 milliseconds CLAP CLAP CLAP CLAP ...
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有