Spring 基于XML配置的 AOP 编程案例

编写程序实现以下功能:
1.日志功能:在程序执行期间,追踪正在发生的活动(打印出调用的方法,以及参数的参数值)
2.验证功能:希望计算器只能处理正数的计算,当有负数参与运算时,给出提示说明

分析:首先采用OOP编程技术,然后采用AOP技术附加需求

OOP编程技术
  1. 编写业务逻辑接口
package com.edu.aop.xml;
public interface ArithmeticCalculator {
	public int add(int i, int j);
	public int sub(int i, int j);	
	public int mul(int i, int j);
	public int div(int i, int j);	
}
  1. 接口的实现类
package com.edu.aop.xml;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i / j;
		return result;
	}

}
  1. 添加bean配置信息
<bean id="abcd" class="com.edu.aop.xml.ArithmeticCalculatorImpl"></bean>
  1. 设计测试程序
package com.edu.aop.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main_xml {	
	public static void main(String[] args) {
		//(1)创建IoC容器
		String pathName="applicationContext_xml.xml";		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(pathName);
		//(2)从容器IoC中获取对象(在类中已经采用注释)
		ArithmeticCalculator ac = (ArithmeticCalculator) ctx.getBean("abcd");		
		//(3)使用对象
		int result = ac.add(10, 20);
		System.out.println("result:" + result);				
		result = ac.div(-21, 3);
		System.out.println("result:" + result);
		((ClassPathXmlApplicationContext)ctx).close();
	}
}
AOP编程技术

设计 “切面” 要完成功能的 “切面类”

package com.edu.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class LoggingAspect {	
	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		Object [] args = joinPoint.getArgs();		
		System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
		
	}	
	
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName + " ends");
		
	}
	
}

修改配置文件,添加 AOP 如下的配置信息:

<!-- 配置切面类的一个bean -->
	<bean id="logging" class="com.edu.aop.xml.LoggingAspect"></bean>
	<!-- 配置 AOP -->
	<aop:config>
		<!-- 配置切面及通知 -->
		<aop:aspect ref="logging" >
			<aop:before method="beforeMethod"
				pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
			<aop:after method="afterMethod"
				pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
		</aop:aspect>
	</aop:config>

设置一个切面类,判定实参是否有负数:

package com.edu.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class VlidationAspect {

	public void validateArgs(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		 //取得参数
		Object [] args = joinPoint.getArgs();
		System.out.println("-->validate:" + Arrays.asList(args));
		if (args != null && args.length > 0) {
	            for(int i=0;i<args.length;++i){
	            	if(((Integer)args[i]).intValue()<0){
	            		System.out.println("警告:方法"+methodName+"()第"+i+"参数为负数:" +args[i]);	            		
	            	}
	            }
	        } 
	}
}

再次修改配置文件,添加新的切面类的配置:

<?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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

	<bean id="abcd" class="com.edu.aop.xml.ArithmeticCalculatorImpl"></bean>

	<!-- 配置切面类的一个bean -->
	<bean id="logging" class="com.edu.aop.xml.LoggingAspect"></bean>
	<bean id="xyz" class="com.edu.aop.xml.VlidationAspect"></bean>
	<!-- 配置 AOP -->
	<aop:config>
		<!-- 配置切面及通知 -->
		<aop:aspect ref="logging" order="2">
			<aop:before method="beforeMethod"
				pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
			<aop:after method="afterMethod"
				pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
		</aop:aspect>

        <!-- 配置第2个切面及通知 -->
		<aop:aspect ref="xyz" order="1">		
		<aop:before method="validateArgs"
				pointcut="execution(* com.edu.aop.xml.ArithmeticCalculator.*(..))" />
		</aop:aspect>
	</aop:config>
</beans>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫余

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值