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

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

希望对你有所帮助!


http://www.kler.cn/a/580398.html

相关文章:

  • 初阶数据结构(C语言实现)——3.4带头双向循环链表详解(定义、增、删、查、改)
  • 今日头条文章爬虫教程
  • 能源电力行业中,利用物联网技术实现“一塔一档”
  • React基础之项目实战
  • SpringBoot 集成 Caffeine 实现本地缓存
  • 处理动态分页:自动翻页与增量数据抓取策略-数据议事厅
  • C51串口初始化及波特率设置
  • SOAP与NETCONF:协议特性、场景与应用全景解析
  • Apache XTable:在数据湖仓一体中推进数据互作性
  • 面试题之webpack file-loader和url-loader
  • 1688店铺所有商品数据接口详解
  • python文本处理pdfminer库安装与使用
  • LeetCode热题100中的背包问题
  • 基于大数据的商品数据可视化及推荐系统
  • 鸿蒙应用开发—数据持久化之SQLite
  • RangeError: Maximum call stack size exceeded
  • 【人工智能】随机森林的智慧:集成学习的理论与实践
  • 元脑服务器的创新应用:浪潮信息引领AI计算新时代
  • 物联网-电路局“一杆一档”管理
  • 【开源宝藏】Spring Trace 一种轻量级的日志追踪新方式