基于XML配置文件的方式配置AOP

一:配置文件配置AOP

1.创建ArtithmeticCalculator接口并实现

package com.dhx.spring.aop.xml;

public interface ArtithmeticCalculator {
	 //加法运算
	 int add(int i,int j);
	 //减法运算
	 int sub(int i,int j);
	 //乘法运算
	 int mul(int i,int j);
	 //除法运算
	 int div(int i,int j);

}
package com.dhx.spring.aop.xml;

import org.springframework.stereotype.Component;

public class ArtithmeticCalculatorImpl implements ArtithmeticCalculator {

	@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;
	}

}

2.创建一个LoggingAspect类

package com.dhx.spring.aop.xml;

import java.util.Arrays;
import java.util.List;

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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

public class LoggingAspect {
	
	public void beforeMethod(JoinPoint joinPoint) {
		//方法名
		String methodName=joinPoint.getSignature().getName();
		//参数
		List<Object> args=Arrays.asList(joinPoint.getArgs());
		System.out.println("the method "+methodName+" begins with "+args);
	}
	
	public void afterMethod(JoinPoint joinPoint) {
		//方法名
		String methodName=joinPoint.getSignature().getName();
		System.out.println("the method "+methodName+" ends");
	}
	
	public void afterReturningMethod(JoinPoint joinPoint,Object result) {
		//方法名
		String methodName=joinPoint.getSignature().getName();
		System.out.println("the method "+methodName+" ends with "+result);
	}
	
	
	
	public void afterThrowing(JoinPoint joinPoint,Exception ex) {
		//方法名
		String methodName=joinPoint.getSignature().getName();
		System.out.println("the method "+methodName+"occurs execption"+ex);
	}
	
	public Object aroundMethod(ProceedingJoinPoint pjd) {
		Object result=null;
		String methodName=pjd.getSignature().getName();//方法名
		try {
			//前置通知
			System.out.println("the method "+methodName+" begins with "+Arrays.asList(pjd.getArgs()));
			result=pjd.proceed();
			//结果通知
			System.out.println("the method"+methodName+" ends "+result);
		} catch (Throwable e) {
			//异常通知
			System.out.println("the method "+methodName+" occurs execption:"+e);
			throw new RuntimeException(e);
		}
		//后置通知
		System.out.println("the method "+methodName+" ends");
		return result;
	}
}

3.创建一个VidationAspect类

package com.dhx.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
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 VidationAspect {
	
	public void declareJoinPointExpression() {
		
	}
	
	public void vidationMethod(JoinPoint joinPoint) {
		System.out.println("-->VidationAspect:"+Arrays.asList(joinPoint.getArgs()));
	}

}

4.配置文件中配置AOP

<?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">

	<!-- 配置ArtithmeticCalculatorImpl的bean -->
	<bean id="artithmeticCalculator"
		class="com.dhx.spring.aop.xml.ArtithmeticCalculatorImpl"></bean>

	<!--配置LoggingAspect的bean -->
	<bean id="loggingAspect"
		class="com.dhx.spring.aop.xml.LoggingAspect"></bean>

	<!-- 配置VidationAspect的bean -->
	<bean id="vidationAspect"
		class="com.dhx.spring.aop.xml.VidationAspect"></bean>

	<!-- 配置AOP -->
	<aop:config>
		<!-- 配置切点表达式 -->
		<aop:pointcut
			expression="execution(* com.dhx.spring.aop.xml.ArtithmeticCalculator.*(int ,int ))"
			id="pointcut" />
		<!--配置切面和通知 -->
		<aop:aspect ref="loggingAspect" order="2">
		<!--  
			//前置通知
			<aop:before method="beforeMethod" pointcut-ref="pointcut" />
			//结果通知
			<aop:after-returning
				method="afterReturningMethod" pointcut-ref="pointcut"
				returning="result" />
			//异常通知
			<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"
			throwing="ex" />
			//返回通知
			<aop:after method="afterMethod" pointcut-ref="pointcut"/>
		-->
		<!-- 环绕通知 -->
		<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
		</aop:aspect>

		<!-- 配置切面和通知 -->
		<aop:aspect ref="vidationAspect" order="1">
			<aop:before method="vidationMethod"
				pointcut-ref="pointcut" />
		</aop:aspect>
	</aop:config>
</beans>

5测试

package com.dhx.spring.aop.xml;

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


public class Main {

	public static void main(String[] args) {
		//1.创建spring 的IOC容器
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-xml.xml");
		//2.从IOC容器中获取Bean的实例
		ArtithmeticCalculator artithmeticCalculator=ctx.getBean(ArtithmeticCalculator.class);
		//3.使用Bean
		int result=artithmeticCalculator.add(3, 6);
		System.out.println("result: "+result);
		
		result=artithmeticCalculator.div(12, 6);
		System.out.println("result: "+result);
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值