Spring 配置AOP Spring注解配置AOP Spring XML 配置AOP Spring Aspect AOP
一、前言
Spring的特性之一AOP,老生常谈的问题,今天来记录下,Spring 框架中如何配置AOP的问题,注意下哈,非Spring Boot哈; 同时本文以 AOP切面中的表达式 @annotation 为准,还有其他多种类型,请自行了解。
二、依赖pom 和 基础配置
1、Sping 和 Aspect
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
2、springmvc.xml 中增加文件头
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
三、AOP xml形式配置
1、创建 Log注解
/**
* description: Log 注解
* @version v1.0
* @author w
* @date 2021年5月12日上午9:45:15
**/
@Target(value = { ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Log {
String name() default "";
}
2、创建 LogAspect 类, 配置相关切点和增加方法
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/**
* description: AOP xml形式配置
* @version v1.0
* @author w
* @date 2021年5月12日上午9:47:04
**/
@Component
public class LogAspect {
/**
* description: 切点
* @return void
* @version v1.0
* @author w
* @date 2021年5月12日 上午9:47:52
*/
public void pointCut() {
}
public Object arounds(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Log log = methodSignature.getMethod().getAnnotation(Log.class);
// 方法执行前
System.out.println("开始日志记录:" + log.name());
// 方法执行
try {
return joinPoint.proceed();
} catch (Throwable e) {
return e ;
}finally {
// 方法执行后
System.out.println("日志记录执行完毕 ===== ");
}
}
}
3、springmvc.xml 中配置AOP 使用
<!-- 配置AOP xml 形式 -->
<aop:config>
<aop:pointcut expression="@annotation(com.runcode.aspect.Log)" id="point"/>
<aop:aspect ref="logAspect" order="10">
<aop:around method="arounds" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
<!-- 配置AOP xml end -->
4、创建 AspectController
@Log(name="web层")
@RequestMapping(value= "/aspectController")
@ResponseBody
public String test(String name) {
System.out.println("AspectController : "+ name);
return "AspectController : "+ name;
}
5、请求测试,查看控制台输出值情况; 方法得到增强,AOP 成功
开始日志记录:web层
AspectController : null
日志记录执行完毕 =====
四、AOP 注解形式配置
1、创建 LogAnon 注解
/**
* description: LogAnon 注解
* @version v1.0
* @author w
* @date 2021年5月12日上午9:45:15
**/
@Target(value = { ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LogAnon {
String name() default "";
}
2、创建 LogAspectAnon 类,使用注解形式配置切点和增强方法
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* description: AOP 注解形式配置
* @version v1.0
* @author w
* @date 2021年5月12日上午10:07:24
**/
@Aspect
@Component
// 有多个AOP配置,可使用order来确定执行顺序
@Order(value = 1)
// 可以在xml配置文件中,去掉 <aop:aspectj-autoproxy/> 标签,使用 该注解
// @EnableAspectJAutoProxy
public class LogAspectAnon {
/**
* description: 配置切点
* @return void
* @version v1.0
* @author w
* @date 2021年5月12日 上午10:15:20
*/
@Pointcut(value = "@annotation(com.runcode.aspect.LogAnon)")
public void pointCuts() {
}
@Around(value = "pointCuts()")
public Object arounds(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
LogAnon log = methodSignature.getMethod().getAnnotation(LogAnon.class);
// 方法执行前
System.out.println("【注解】开始日志记录:" + log.name());
// 方法执行
try {
return joinPoint.proceed();
} catch (Throwable e) {
return e ;
}finally {
// 方法执行后
System.out.println("【注解】日志记录执行完毕 ===== ");
}
}
}
3、springmvc.xml 中配置 AOP **启用注解**形式实现 (没错,就一行代码)
<!-- 配置AOP 注解形式 -->
<!-- 在类上面使用 @EnableAspectJAutoProxy 注解,可以去掉该标签配置 -->
<aop:aspectj-autoproxy/>
4、使用 AspectController 进行测试
@LogAnon(name = "web层 注解")
@RequestMapping(value= "/aspectController")
@ResponseBody
public String test(String name) {
System.out.println("AspectController : "+ name);
return "AspectController : "+ name;
}
5、请求测试,查看控制台输出值情况; 方法得到增强,AOP 成功
【注解】开始日志记录:web层 注解
AspectController : null
【注解】日志记录执行完毕 =====
五、总结
1、本文简单的记录了,Spring 实现AOP的过程;基于注解的形式实现。([Spring 切点][Spring])
2、若使用注解的形式实现AOP,且在传统SSM项目中,需要分别在 spring.xml 和springmvc.xml 配置文件中,启用注解([spring 和 springMVC的容器问题][spring _ springMVC]) ; 使用配置文件形式,则不需要区分。
3、在Spring Boot项目中,使用注解实现AOP,不需要启用注解的步骤。
还没有评论,来说两句吧...