Spring 面向切面编程 XML 配置实现

Spring 支持AOP ,并且可以通过XML配置来实现。

bash 复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!-- 定义目标对象 -->
    
      <context:component-scan base-package="com" />
    

    <!-- 定义切面 -->
     <bean id="logAspect" class="com.aspect.LogAspect"></bean>
 
     <aop:config>
          <aop:aspect ref="logAspect">
              <aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="myAspect" />
              <aop:before  method="beforePrintLog" pointcut-ref="myAspect"></aop:before>
              <aop:after  method="afterPrintLog" pointcut-ref="myAspect"></aop:after>
              <aop:after-throwing  method="afterThrowingPrintLog" pointcut-ref="myAspect"></aop:after-throwing>
          </aop:aspect>
     
     </aop:config>
</beans>

相关标签介绍:

aop:config 注明开始配置aop ,是配置的开始标签

aop:aspect 配置切面 ref 属性是引用相关切面类Bean的id

aop:point-cut 定义切点 expression 是具体的表达式 id 是切点的标识

aop:before 定义前置通知 method是要执行的方法 pointcut-ref为引用的aop-point-cut 定义的id

aop:after 定义最终通知

aop:around 定义环绕通知

aop:after-throwing 定义返回异常的通知

aop:after-returning 定义正常返回的通知

切面类:

java 复制代码
package com.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

public class LogAspect {
	
	
	public void beforePrintLog() {
		System.out.println("LogAspectbeforePrintLog");
	}
	

	public void afterReturnPrintLog() {
		System.out.println("LogAspectafterReturnPrintLog");
	}
	
	
	public void afterThrowingPrintLog() {
		System.out.println("LogAspectafterThrowingPrintLog");
	}
	
	
	public void afterPrintLog() {
		System.out.println("LogAspectafterPrintLog");
	}
	
	
	public void aroundPrintLog() {
		System.out.println("aroundPrintLog");
	}
}

切点类必须是实现某个接口才行,SpringAOP代理实现机制包含Spring GGLIB 方式和JDK动态代理方式,默认的是JDK动态代理的方式,但是这种实现方式切点类我们的必须要实现某个接口 jdk9以及之后,切点类必须要实现接口

希望对你有所帮助!

相关推荐
hqxstudying13 分钟前
Java异常处理
java·开发语言·安全·异常
我命由我123453 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
武子康5 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
YuTaoShao8 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
源码_V_saaskw8 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
超浪的晨8 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
双力臂4049 小时前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
心之语歌9 小时前
Spring AI MCP 客户端
人工智能·spring·github
Edingbrugh.南空9 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
QQ_43766431410 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++