spring的AOP_基于XML实现AOP的过程

博客介绍了AOP切面配置相关内容,通过代码展示了参考logAspect bean或类,定义了切面筛选条件,还分别配置了在logPointCut切面下调用logStart、logEnd和logAround方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   <!-- 打开Spring的Annotation支持 -->
   <context:annotation-config/>
   <!-- 设定Spring 去哪些包中找Annotation -->
   <context:component-scan base-package="org.zttc.itat.spring"/>
   
   <aop:config>
   <!-- 定义切面 -->
   		<aop:aspect id="myLogAspect" ref="logAspect">
   		<!-- 在哪些位置加入相应的Aspect -->
   			<aop:pointcut id="logPointCut" expression="execution(* org.zttc.itat.spring.dao.*.add*(..))||
   							execution(* org.zttc.itat.spring.dao.*.delete*(..))||
   							execution(* org.zttc.itat.spring.dao.*.update*(..))"/>
   			<aop:before method="logStart" pointcut-ref="logPointCut"/>
   			<aop:after method="logEnd" pointcut-ref="logPointCut"/>
   			<aop:around method="logAround" pointcut-ref="logPointCut"/>
   		</aop:aspect>
   </aop:config>
   
</beans>

<aop:aspect id="myLogAspect" ref="logAspect"> 参考对应的 logAspect bean 或者 logAspect 类。

logAspect类如下:

package org.zttc.itat.spring.proxy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component("logAspect")//让这个切面类被Spring所管理
public class LogAspect {
	
	public void logStart(JoinPoint jp) {
		//得到执行的对象
		System.out.println(jp.getTarget());
		//得到执行的方法
		System.out.println(jp.getSignature().getName());
		Logger.info("加入日志");
	}
	public void logEnd(JoinPoint jp) {
		Logger.info("方法调用结束加入日志");
	}
	
	public void logAround(ProceedingJoinPoint pjp) throws Throwable {
		Logger.info("开始在Around中加入日志");
		pjp.proceed();//执行程序
		Logger.info("结束Around");
	}
	
}

 

<aop:pointcut id="logPointCut" expression="execution(* org.zttc.itat.spring.dao.*.add*(..))||
execution(* org.zttc.itat.spring.dao.*.delete*(..))||
execution(* org.zttc.itat.spring.dao.*.update*(..))"/>

定义了切面的筛选条件,

<aop:before method="logStart" pointcut-ref="logPointCut"/>,定义了 logPointCut 切面下调用方法 logStart,

<aop:after method="logEnd" pointcut-ref="logPointCut"/>,定义了 logPointCut 切面下调用方法 logEnd,

<aop:around method="logAround" pointcut-ref="logPointCut"/>,定义了 logPointCut 切面下调用方法 logAround。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值