[10] AOP的注解配置
1、关于配置文件
首先在因为要使用到扫描功能,所以xml的头文件中除了引入bean和aop之外,还要引入context才行:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
</beans> x
11
1
2
<beans xmlns="http://www.springframework.org/schema/beans"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/beans7
http://www.springframework.org/schema/beans/spring-beans.xsd8
http://www.springframework.org/schema/aop9
http://www.springframework.org/schema/aop/spring-aop.xsd10
http://www.springframework.org/schema/context11
http://www.springframework.org/schema/context/spring-context.xsd">12
13
...14
15
</beans> 既然使用注解,那么在配置文件中需要开启扫描配置以注册bean组件;同时Spring中使用了aspectj包的@Aspect注解标注当前组件为切面,所以同时还需要在配置文件中配置实用aspectj的自动代理模式。如下:
<!-- 开启bean组件扫描 -->
<context:component-scan base-package="dulk.learn"></context:component-scan>
<!-- 启用自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>1
1
<!-- 开启bean组件扫描 -->2
<context:component-scan base-package="dulk.learn"></context:component-scan>3
<!-- 启用自动代理 -->4
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>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
@Aspect
public 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
13
14
public class Section {15
16
("execution(* dulk.learn..*.*(..))")17
public void doBefore(JoinPoint point) {18
System.out.println("doBefore");19
}20
21
(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
(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")28
public void doThrow(JoinPoint point, Throwable e) {29
System.out.println("doThrow");30
}31
32
("execution(* dulk.learn..*.*(..))")33
public void doAfter() {34
System.out.println("doAfter");35
}36
37
("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
}
浙公网安备 33010602011771号