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"

<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:mvc="http://www.springframework.org/schema/mvc"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

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 切点

 

    2、若使用注解的形式实现AOP,且在传统SSM项目中,需要分别在 spring.xml 和springmvc.xml 配置文件中,启用注解(spring 和 springMVC的容器问题) ; 使用配置文件形式,则不需要区分。

 

    3、在Spring Boot项目中,使用注解实现AOP,不需要启用注解的步骤。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Spring,基于XML配置AOP的步骤如下: 1. 添加依赖并准备环境。 2. 创建通知类和接口,并实现方法。 3. 配置spring.xml文件。 4. 执行测试。 具体步骤如下: 1. 创建通知类和接口,并实现方法。 2. 在spring.xml文件配置AOP。 3. 使用aop:config标签开始AOP配置。 4. 使用aop:aspect标签配置切面,其id属性是给切面提供一个唯一标识,ref属性是指定通知类bean的Id。 5. 在aop:aspect标签内部使用对应标签来配置通知的类型,例如使用aop:before标签配置前置通知。 6. 在aop:before标签,使用method属性指定Logger类哪个方法是前置通知,使用pointcut属性指定切入点表达式,该表达式指定了对业务层哪些方法进行增强。 7. 切入点表达式的写法可以使用关键字execution和具体的表达式来指定方法的访问修饰符、返回值、包名、类名和方法名等。 8. 在通知类添加环绕通知方法,使用ProceedingJoinPoint接口作为环绕通知的方法参数,该接口提供了proceed()方法,用于明确调用切入点方法。 9. 在环绕通知方法,可以通过proceedingJoinPoint.proceed(args)调用切入点方法,并在方法执行前后进行相应的操作。 10. 最后,执行测试来验证AOP配置的效果。 以上是基于XML配置AOP的步骤。通过配置AOP,可以在指定的切入点方法执行前后,或者在方法执行过程进行增强操作。 #### 引用[.reference_title] - *1* *3* [Spring之基于XML配置AOP](https://blog.csdn.net/qq_38628046/article/details/108065715)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring基于XMLAOP配置](https://blog.csdn.net/weixin_45430616/article/details/104101117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HaHa_Sir

讨饭啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值