Spring框架:基于XML配置AOP

基于配置文件配置AOP

同理我们需要导入spring需要的jar包,这个步骤就不在重复了,和之前一样

我们新建一个包:com.yorkmass.spring.aop.xml,下面新建1个接口和4个类,在src目录新建配置文件命名为:

applicationContext-xml.xml,打开勾选aop、beans命名空间

程序结构如下:

 ArithmeticCalculator接口

package com.yorkmass.spring.aop.xml;

public interface ArithmeticCalculator {
	int add(int i,int j);
	int sub(int i,int j);
	int mul(int i,int j);
	int div(int i,int j);
	
}

ArithmeticCalculatorImpl类

package com.yorkmass.spring.aop.xml;

import org.springframework.stereotype.Component;


public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result=i+j;
//		System.out.println("-->"+result);
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result=i-j;
//		System.out.println("-->"+result);
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result=i*j;
//		System.out.println("-->"+result);
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result=i/j;
//		System.out.println("-->"+result);
		return result;
	}

}

LoggingAspect类

package com.yorkmass.spring.aop.xml;

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.Before;
import org.aspectj.lang.annotation.Pointcut;

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 with ");		
	}
	
	
	public void afterReturning(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+" Throwing:"+ex);
	}
	
	
	public Object aroundMethod(ProceedingJoinPoint pjd){
	
		Object result=null;
		String methodName=pjd.getSignature().getName();
		
		//执行目标方法
	try {
		//前置通知
		System.out.println("The method "+methodName+" begin with: "+Arrays.asList(pjd.getArgs()));
		result=pjd.proceed();
		//返回通知
		System.out.println("The method "+methodName+" end with:"+result);
		
	} catch (Throwable e) {
		// TODO: handle exception
		//异常通知
		System.out.println("The method "+methodName+" occurs exception:"+e);
		throw new RuntimeException(e);
	}
	//后置通知
	System.out.println("The method "+methodName+" end with");
	return result;
	}
	
}

VlidationAspect类

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

public class VlidationAspect {

	public void validateArgs(JoinPoint joinPoint){
		System.out.println("-->validate"+Arrays.asList(joinPoint.getArgs()));
		
	}

}

applicationContext-xml.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"
	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 -->
<bean id="arithmeticCalculator"
class="com.yorkmass.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
<!-- 配置切面的bean -->
<bean id="loggingAspect"
class="com.yorkmass.spring.aop.xml.LoggingAspect"></bean>

<bean id="VlidationAspect"
class="com.yorkmass.spring.aop.xml.VlidationAspect"></bean>

<!-- 配置AOP -->
<aop:config>
	<!-- 配置切入点表达式 -->
	<aop:pointcut expression="execution(* com.yorkmass.spring.aop.xml.ArithmeticCalculator.*(..))" 
	id="pointcut"/>
	<!-- 配置切面及通知 -->
	<aop:aspect ref="loggingAspect" order="2">
	<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
	<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
	<aop:after method="afterMethod" pointcut-ref="pointcut"/>
	<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
	<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
	</aop:aspect>
	
	<aop:aspect ref="VlidationAspect" order="1">
	<aop:before method="validateArgs" pointcut-ref="pointcut"/>
	</aop:aspect>
	
	
</aop:config>
</beans>

测试类(Main)

package com.yorkmass.spring.aop.xml;

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

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-xml.xml");
		ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator)ctx.getBean("arithmeticCalculator");
		int result=arithmeticCalculator.add(1, 5);
		System.out.println("result:"+result);
		result=arithmeticCalculator.div(1000, 20);
		System.out.println("result:"+result);
	}
	


}

运行结果:

一月 19, 2019 4:29:55 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c2faae: startup date [Sat Jan 19 16:29:55 CST 2019]; root of context hierarchy
一月 19, 2019 4:29:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-xml.xml]
-->validate[1, 5]
The method add begins with [1, 5]
The method add begin with: [1, 5]
The method add end with:6
The method add end with
The method add ends with 
The method add ends with 6
result:6
-->validate[1000, 20]
The method div begins with [1000, 20]
The method div begin with: [1000, 20]
The method div end with:50
The method div end with
The method div ends with 
The method div ends with 50
result:50

 

posted @ 2019-01-19 16:31  yorkmass  阅读(215)  评论(0)    收藏  举报