Spring AOP 的引入(三 基于xml)

还是一样的需求

在执行计算的时候,给控制台答应的格式为


begain  method  with [ args1,args2 ] 


end method with result

这次使用Spring AOP

定义接口:

package com.dxf.ArithmethicCalculatorSpringAOP;

public interface ArithmethicCalculator {

	int add(int a, int b);

	int sub(int a, int b);

	int sul(int a, int b);

	int div(int a, int b);

}

定义实现类:

package com.dxf.ArithmethicCalculatorSpringAOP;

import org.springframework.stereotype.Component;

@Component
public class ArithmethicCalculatorimp implements ArithmethicCalculator {

	public int add(int a, int b) {

		int result = a + b;

		return result;
	}

	public int sub(int a, int b) {
		int result = a - b;

		return result;
	}

	public int sul(int a, int b) {
		int result = a * b;

		return result;
	}

	public int div(int a, int b) {
		int result = a / b;

		return result;
	}

}
编写切面类
package com.dxf.ArithmethicCalculatorSpringAOPXml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

public class ArithmethicCalculatorAspect {

	public void methodBefor(JoinPoint joinPoint) {

		System.out
				.println("begain " + joinPoint.getSignature().getName() + " with" + Arrays.asList(joinPoint.getArgs()));

	}

	public void methodAfter(JoinPoint joinPoint) {

		System.out.println("end " + joinPoint.getSignature().getName() + " with" + Arrays.asList(joinPoint.getArgs()));

	}

	public void methodThrow(JoinPoint joinPoint, Exception e) {
		System.out.println(joinPoint.getSignature().getName() + "发生异常: " + e.getMessage());

	}

	public void methodAfterReturn(JoinPoint joinPoint, Object result) {

		// System.out.println(value);
		System.out.println("methodAfterReturn.....result " + result);

	}

	// 环绕通知,此通知就类似于一个代理类,上面的四种通知都在本通知中可以实现
	public Object methodAroud(ProceedingJoinPoint joinPoint) {

		Object[] args = joinPoint.getArgs();// 获取参数列表
		String method = joinPoint.getSignature().getName();// 获取方法名
		Object result = null;
		try {

			// 前置通知
			System.out.println("methodAroud before");
			result = joinPoint.proceed();// 执行函数
			System.out.println("methodAroud after");
			// 后置通知
		} catch (Throwable e) {
			// 异常通知
			System.out.println("methodAroud Throwable" + e.getMessage());
			e.printStackTrace();
		}
		// 返回通知
		System.out.println("methodAroud afterReturn " + result);

		return result;

	}

}

编写xml配置文件:

<?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"
	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-4.0.xsd">


	<bean id="arithmethicCalculator"
		class="com.dxf.ArithmethicCalculatorSpringAOPXml.ArithmethicCalculatorimp">
	</bean>

	<bean id="arithmethicCalculatorAspect"
		class="com.dxf.ArithmethicCalculatorSpringAOPXml.ArithmethicCalculatorAspect">
	</bean>

	<!-- 配置Aop -->
	<aop:config>
		<!-- 配置切点表达式 -->
		<aop:pointcut
			expression="execution(* com.dxf.ArithmethicCalculatorSpringAOPXml.ArithmethicCalculatorimp.*(int ,int))"
			id="pointcut" />

		<!-- 配置切面及通知 -->
		<aop:aspect ref="arithmethicCalculatorAspect">
			<!-- 前置通知 -->

			<aop:before method="methodBefor" pointcut-ref="pointcut" />
			<!-- 后置通知 -->
			<aop:after method="methodAfter" pointcut-ref="pointcut" />

			<!-- 返回通知 -->

			<aop:after-returning method="methodAfterReturn"
				pointcut-ref="pointcut" returning="result" />

			<!-- 异常通知 -->

			<aop:after-throwing method="methodThrow"
				pointcut-ref="pointcut" throwing="e" />

			<!-- 环绕通知 -->

			<aop:around method="methodAroud" pointcut-ref="pointcut" />

		</aop:aspect>


	</aop:config>

</beans>

测试类:

package com.dxf.ArithmethicCalculatorSpringAOPXml;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {

		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"application-aop-xml.xml");

		ArithmethicCalculator arithmethicCalculator = applicationContext.getBean(ArithmethicCalculator.class);

		System.out.println(arithmethicCalculator.div(1, 1));

	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值