当前位置: 首页 > news >正文

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

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

<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 定义正常返回的通知

切面类:

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以及之后,切点类必须要实现接口

希望对你有所帮助!

相关文章:

  • LabVIEW变频器谐波分析系统
  • 【leetcode hot 100 25】K个一组翻转链表
  • 使用SDKMAN!安装springboot
  • kettle-打不开提示Could not find the main class
  • nextjs15简要介绍以及配置eslint和prettier
  • halcon deeplearn 语义分割经验分享 1
  • Gazebo直接构建仿真世界
  • VSCode-Server 在 Linux 容器中的手动安装指南
  • 【技海登峰】Kafka漫谈系列(八)Controller:Zookeeper模式与KRaft模式
  • 山东2025年网络管理员报名工作经验要求与所需材料
  • 无标签数据增强+高效注意力GAN:基于CARLA的夜间车辆检测精度跃升
  • 责任链模式的C++实现示例
  • 项目中async和await的应用场景
  • PHPCMS V9 登录加密改造
  • 【redis】应用场景:共享会话和手机验证码
  • Quickwit+Jaeger+Prometheus+Grafana搭建Java日志管理平台
  • 使用AI一步一步实现若依前端(7)
  • Flutter 基础组件 Text 详解
  • 工作记录 2017-01-05
  • 算法面试题深度解析:LeetCode 2012.数组元素的美丽值求和计算与多方案对比
  • 综艺还有怎样的新可能?挖掘小众文化领域
  • 上海消防全面推行“检查码”,会同相关部门推行“综合查一次”
  • 眉山“笑气”迷局:草莓熊瓶背后的隐秘与危机
  • 见微知沪|优化营商环境,上海为何要当“细节控”自我加压?
  • 国务院安委会办公室印发通知:坚决防范遏制重特大事故发生
  • 央行:5月15日起下调金融机构存款准备金率0.5个百分点