第1集 Spring AOP注解基础准备
简介:讲解Spring AOP注解的基础准备
- 声明切⾯类 @Aspect(切⾯): 通常是⼀个类,⾥⾯可以定义切⼊点和通知
- 配置切⼊点和通知 (第二章 LogAdvice类)
第2集 开启Spring AOP注解配置和扫描
简介:开启Spring AOP注解和扫描
- 开启SpringAOP注解配置
@Configuration //需要得到一个注解配置类
@ComponentScan("xj.com") //扫com以下的包 相当于 context.scan("xj.com") 操作
//开启spring对aspect支持
@EnableAspectJAutoProxy
public class AnnotationAopConfiguration {
//这是一个AOP的配置类
}
切面类:需要加Component,不加的话扫描不到这个文件
@Component //注册bean
@Aspect //告诉Spring这是一个切面类,里面可以定义切入点和通知方法
public class LogAdvice { //注解完成切面功能
//定义一个切入点
@Pointcut(value = "execution(* xj.com.springDemo.service.impl.VideoServiceImpl.*(..))")
public void PointCut(){ }
//定义一个前置通知
@Before("PointCut()")
public void befor(){
System.out.println("前置增强处理");
}
}
VideoService实现类:也需要加Service注解,不然扫描不到这个bean
@Service("videoService")
public class VideoServiceImpl implements VideoService {
public int addOrder(VideoOrder videoOrder) {
System.out.println("使用addOrder方法");
return 0;
}
public Long delOrder(VideoOrder videoOrder) {
System.out.println("使用delOrder方法");
return null;
}
public List<VideoOrder> findOrderById() {
System.out.println("使用findOrderById方法");
return null;
}
public int updateOrder(VideoOrder videoOrder) {
System.out.println("使用updateOrder方法");
return 0;
}
public int save(Video video) {
System.out.println("存一条视频");
return 0;
}
}
测试类;注意getBean不在是接口了,而是我们AOP的配置类 使用这个我们就不需要写refresh操作,还有scan操作,因为在配置类中已全部用注解配置了。
/**
* 使用注解打印日志处理
*/
@Test
public void Test3(){
//1.需要配置AOP配置类
//2.使用注解方式创建context
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationAopConfiguration.class);
//3.获得一个配置类中的bean
VideoService videoService =(VideoService) context.getBean("videoService");
//4.执行方法 同时会被通知切入进去
videoService.addOrder(new VideoOrder());
}
运行结果:
第3集 AOP案例实战之环绕通知统计接⼝耗时
简介:通过AOP的环绕通知统计⽅法调⽤耗时
- 配置环绕通知:打印⽅法请求耗时时间
-
环绕通知获取⽬标⽅法和参数
环绕代码:核心代码(
注意目标类返回什么类型 around就需要返回什么类型)
//使用环绕通知
@Around(value = "execution(* xj.com.springDemo.service.impl.VideoServiceImpl.*(..))")
public int around(JoinPoint joinPoint){ //joinPoint可以查看调用的方法,以及调用者
//查看调用者
Object target = joinPoint.getTarget().getClass().getName();
//xj.com.springDemo.service.impl.VideoServiceImpl
System.out.println("调用者="+target);
//目标方法签名--也就是调用的方法名字
System.out.println("调用方法="+joinPoint.getSignature());
//获取方法参数
Object[] args = joinPoint.getArgs();
long start = System.currentTimeMillis();
System.out.println("环绕通知 环绕前=========");
//执⾏连接点的⽅法
try {
((ProceedingJoinPoint) joinPoint).proceed(); //执行实现类的方法
} catch (Throwable throwable) {
throwable.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("环绕通知 环绕后=========");
System.out.println("时间消耗"+(end-start));
return 1;
}
实现类:
public class VideoServiceImpl implements VideoService {
public int addOrder(VideoOrder videoOrder) {
System.out.println("使用addOrder方法");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}
}
测试类:
@Test
public void Test3(){
//1.需要配置AOP配置类
//2.使用注解方式创建context
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationAopConfiguration.class);
//3.获得一个配置类中的bean
VideoService videoService =(VideoService) context.getBean("videoService");
//4.执行方法 同时会被通知切入进去
int i = videoService.addOrder(new VideoOrder());
System.out.println("返回值"+i);
}
运行结果:可以看出返回的是around环绕类中的返回值,around会覆盖实现类中的返回值