博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[10] AOP的注解配置
阅读量:4308 次
发布时间:2019-06-06

本文共 4295 字,大约阅读时间需要 14 分钟。

1、关于配置文件

首先在因为要使用到扫描功能,所以xml的头文件中除了引入bean和aop之外,还要引入context才行:
...
x
11
 
1
2
 
3
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
      xmlns:context="http://www.springframework.org/schema/context"
5
      xmlns:aop="http://www.springframework.org/schema/aop"
6
      xsi:schemaLocation="http://www.springframework.org/schema/beans
7
      http://www.springframework.org/schema/beans/spring-beans.xsd
8
      http://www.springframework.org/schema/aop
9
      http://www.springframework.org/schema/aop/spring-aop.xsd
10
      http://www.springframework.org/schema/context
11
      http://www.springframework.org/schema/context/spring-context.xsd">
12
   
13
   ...
14
   
15
    
既然使用注解,那么在配置文件中需要开启扫描配置以注册bean组件;同时Spring中使用了aspectj包的@Aspect注解标注当前组件为切面,所以同时还需要在配置文件中配置实用aspectj的自动代理模式。如下:
1
 
1
2
3
4

2、AOP的注解配置

AOP的注解配置方式,对于一个类来说:
  • 通过 @Component 声明该类为bean组件
  • 通过 @Aspect 标记该类为切面
  • 通过注解说明类中函数的通知类型
import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Component@Aspectpublic class Section {    @Before("execution(* dulk.learn..*.*(..))")    public void doBefore(JoinPoint point) {        System.out.println("doBefore");    }    @AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")    public void doAfterReturning(JoinPoint point, Object ret) {        System.out.println("doAfterReturning");        System.out.println("The returning obj is " + ret);    }    @AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")    public void doThrow(JoinPoint point, Throwable e) {        System.out.println("doThrow");    }    @After("execution(* dulk.learn..*.*(..))")    public void doAfter() {        System.out.println("doAfter");    }    @Around("execution(* dulk.learn..*.*(..))")    public void doAround(ProceedingJoinPoint point) {        System.out.println("doAround-dobefore");        try {            Object obj = point.proceed();            System.out.println("doAround-doAfterReturning");        } catch (Throwable throwable) {            System.out.println("doAround-doThrow");        }        System.out.println("doAround-doAfter");    }}
x
1
49
 
1
import org.aspectj.lang.JoinPoint;
2
import org.aspectj.lang.ProceedingJoinPoint;
3
import org.aspectj.lang.annotation.After;
4
import org.aspectj.lang.annotation.AfterReturning;
5
import org.aspectj.lang.annotation.AfterThrowing;
6
import org.aspectj.lang.annotation.Around;
7
import org.aspectj.lang.annotation.Aspect;
8
import org.aspectj.lang.annotation.Before;
9
import org.springframework.stereotype.Component;
10
 
11
 
12
@Component
13
@Aspect
14
public class Section {
15
 
16
   @Before("execution(* dulk.learn..*.*(..))")
17
   public void doBefore(JoinPoint point) {
18
       System.out.println("doBefore");
19
   }
20
 
21
   @AfterReturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
22
   public void doAfterReturning(JoinPoint point, Object ret) {
23
       System.out.println("doAfterReturning");
24
       System.out.println("The returning obj is " + ret);
25
   }
26
 
27
   @AfterThrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
28
   public void doThrow(JoinPoint point, Throwable e) {
29
       System.out.println("doThrow");
30
   }
31
 
32
   @After("execution(* dulk.learn..*.*(..))")
33
   public void doAfter() {
34
       System.out.println("doAfter");
35
   }
36
 
37
   @Around("execution(* dulk.learn..*.*(..))")
38
   public void doAround(ProceedingJoinPoint point) {
39
       System.out.println("doAround-dobefore");
40
       try {
41
           Object obj = point.proceed();
42
           System.out.println("doAround-doAfterReturning");
43
       } catch (Throwable throwable) {
44
           System.out.println("doAround-doThrow");
45
       }
46
       System.out.println("doAround-doAfter");
47
   }
48
 
49
}

转载于:https://www.cnblogs.com/deng-cc/p/9225047.html

你可能感兴趣的文章
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>
Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
查看>>
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
linux下载github中的文件
查看>>
HDP Sandbox里面git clone不了数据(HTTP request failed)【目前还没解决,所以hive的练习先暂时搁置了】
查看>>
动态分区最佳实践(一定要注意实践场景)
查看>>
HIVE—索引、分区和分桶的区别
查看>>
Hive进阶总结(听课总结)
查看>>
大数据领域两大最主流集群管理工具Ambari和Cloudera Manger
查看>>