Spring_AOP_XML使用Aspect实现动态代理(常用) .

Spring_AOP_XML使用Aspect实现动态代理(常用)

XML使用Aspect实现动态代理此方式比较常用,和使用注解最大的好处是我们不用每个方法前面定义横切点上面加入PointCut的说明,在XML中只需要定义一次就可以多出使用。

在上面Spring_AOP_Annotation使用Aspect实现动态代理的基础上修改,去除LogAspect中方法上的注解。

XML文件配置:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">   
  8.   
  9.     <!-- 打开Spring的Annotation的支持 -->  
  10.     <context:annotation-config />  
  11.     <!-- 设定Spring去哪些包中找Annotation -->  
  12.     <context:component-scan base-package="com.spring" />  
  13.     <!-- 打开基于Annotation的aop自动代理 -->  
  14.     <aop:aspectj-autoproxy />  
  15.   
  16.     <aop:config>  
  17.         <!-- 定义切面 -->  
  18.         <aop:aspect id="myLogAspect" ref="logAspect">  
  19.         <!-- 在哪些位置加入相应的Aspect -->  
  20.             <aop:pointcut id="logPointCut"  
  21.                 expression="execution(* com.spring.dao.*.add*(..))||  
  22.                 execution(* com.spring.dao.*.update*(..))||  
  23.                 execution(* com.spring.dao.*.delete*(..))" />  
  24.             <!-- 开始之前加入日志 -->   
  25.             <aop:before method="logStart" pointcut-ref="logPointCut" />  
  26.             <!-- 结束之后加入日志 -->   
  27.             <aop:after method="logEnd" pointcut-ref="logPointCut" />  
  28.             <!-- 执行过程中加入日志 -->      
  29.             <aop:around method="logAround" pointcut-ref="logPointCut"/>  
  30.         </aop:aspect>  
  31.     </aop:config>  
  32.   
  33. </beans>  
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 打开Spring的Annotation的支持 -->
	<context:annotation-config />
	<!-- 设定Spring去哪些包中找Annotation -->
	<context:component-scan base-package="com.spring" />
	<!-- 打开基于Annotation的aop自动代理 -->
	<aop:aspectj-autoproxy />

	<aop:config>
		<!-- 定义切面 -->
		<aop:aspect id="myLogAspect" ref="logAspect">
		<!-- 在哪些位置加入相应的Aspect -->
			<aop:pointcut id="logPointCut"
				expression="execution(* com.spring.dao.*.add*(..))||
				execution(* com.spring.dao.*.update*(..))||
				execution(* com.spring.dao.*.delete*(..))" />
			<!-- 开始之前加入日志 -->	
			<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>

LogAspect类:
  1. package com.spring.proxy;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5. import org.aspectj.lang.annotation.After;  
  6. import org.aspectj.lang.annotation.Around;  
  7. import org.aspectj.lang.annotation.Aspect;  
  8. import org.aspectj.lang.annotation.Before;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. //让这个切面类被Spring所管理   
  12. @Component("logAspect")  
  13. // 声明这是一个切面类   
  14. @Aspect  
  15. public class LogAspect {  
  16.       
  17.     public void logStart(JoinPoint jp) {  
  18.         // 得到执行的对象   
  19.         System.out.println(jp.getTarget());  
  20.         // 得到执行的方法   
  21.         System.out.println(jp.getSignature().getName());  
  22.         Logger.info("方法执行前加入日志,来自LogAspect");  
  23.     }  
  24.       
  25.       
  26.     public void logEnd(JoinPoint jp) {  
  27.         // 得到执行的对象   
  28.         System.out.println(jp.getTarget());  
  29.         // 得到执行的方法   
  30.         System.out.println(jp.getSignature().getName());  
  31.         Logger.info("方法执行后加入日志,来自LogAspect");  
  32.     }  
  33.   
  34.       
  35.     public void logAround(ProceedingJoinPoint pjp) throws Throwable {  
  36.         Logger.info("开始在Around中加入日志,来自LogAspect");  
  37.         //执行程序   
  38.         pjp.proceed();  
  39.         Logger.info("结束Around");  
  40.     }  
  41. }  
package com.spring.proxy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//让这个切面类被Spring所管理
@Component("logAspect")
// 声明这是一个切面类
@Aspect
public class LogAspect {
	
	public void logStart(JoinPoint jp) {
		// 得到执行的对象
		System.out.println(jp.getTarget());
		// 得到执行的方法
		System.out.println(jp.getSignature().getName());
		Logger.info("方法执行前加入日志,来自LogAspect");
	}
	
	
	public void logEnd(JoinPoint jp) {
		// 得到执行的对象
		System.out.println(jp.getTarget());
		// 得到执行的方法
		System.out.println(jp.getSignature().getName());
		Logger.info("方法执行后加入日志,来自LogAspect");
	}

	
	public void logAround(ProceedingJoinPoint pjp) throws Throwable {
		Logger.info("开始在Around中加入日志,来自LogAspect");
		//执行程序
		pjp.proceed();
		Logger.info("结束Around");
	}
}

测试代码及测试结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值