Spring入门学习(使用XML配置文件方式来配置AOP) 第十七节

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011171125/article/details/85922579

Spring入门学习(使用XML配置文件方式来配置AOP)

xml配置文件配置AOP

  1. 使用之前创建的类ArithmeticCalculatorArithmeticCalculatorImpl
  2. 去掉LoggingAspectValidationAspect类中的所有注解
    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..." + args);
    	}
    	
    	public void afterMethod(JoinPoint joinPoint) {
    		String methodName = joinPoint.getSignature().getName();
    		System.out.println("The method "+methodName+" ends...");
    	}
    	
    	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+ " occurs exception: "+ex);
    	}
    	
    	/*
    	public Object aroundMethd(ProceedingJoinPoint pjd){
    		String methodName = pjd.getSignature().getName();
    		Object result = null;
    		try {
    			// 前置通知
    			System.out.println("The method "+methodName+ " begins");
    			result = pjd.proceed();
    			// 返回通知
    			System.out.println("The method "+methodName+" ends with "+result);
    		} catch (Throwable e) {
    			// 异常通知
    			System.out.println("The method "+methodName+ " occurs exception: "+e);
    			e.printStackTrace();
    		}
    		// 后置通知
    		System.out.println("The method "+methodName+ " ends ");
    		return result;
    	}*/
    }
    
    public class ValidationAspect {
    
    	public void validateArgs(JoinPoint joinPoint){
    		System.out.println("-->validate:"+Arrays.asList(joinPoint.getArgs()));
    	}
    }
    
  3. 创建配置文件applicationContext-xml.xml如下:
    在配置文件中,使用<aop:config>来开始配置AOP,<aop:pointcut>配置切点表达式,<aop:aspect>配置切面及通知。
    <?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-4.1.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-4.1.xsd">
    
    	<!-- 配置Bean -->
    	<bean id="calc" class="com.fafa.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
    	
    	<!-- 配置切面的bean -->
    	<bean id="loggingAspect" class="com.fafa.spring.aop.xml.LoggingAspect"></bean>
    	<bean id="validationAspect" class="com.fafa.spring.aop.xml.ValidationAspect"></bean>
    	
    	<!-- 配置AOP -->
    	<aop:config>
    		<!-- 配置切点表达式 -->
    		<aop:pointcut expression="execution(* com.fafa.spring.aop.xml.*.*(..))" id="pointCut"/>
    		<!-- 配置切面及通知 -->
    		<aop:aspect ref="loggingAspect" order="2">
    			<aop:before method="beforeMethod" pointcut-ref="pointCut" />
    			<aop:after method="afterMethod" pointcut-ref="pointCut"/>
    			<aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="ex"/>
    			<aop:after-returning method="afterReturning" pointcut-ref="pointCut" returning="return"/>
    			<!-- 
    			<aop:around method="aroundMethod" pointcut-ref="pointCut"/>
    			 -->
    		</aop:aspect>
    		<aop:aspect ref="validationAspect" order="1">
    			<aop:before method="validateArgs" pointcut-ref="pointCut"/>
    		</aop:aspect>
    	</aop:config>
    </beans>
    
  4. 测试:
    public class Main {
    
    	public static void main(String[] args) {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:applicationContext-xml.xml");
    		ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
    		System.out.println(arithmeticCalculator.getClass().getName());
    		
    		int result = arithmeticCalculator.add(3, 6);
    		System.out.println("result:" + result);
    	
    		result = arithmeticCalculator.div(6, 3);
    		System.out.println("result:" + result);
    		
    //		result = arithmeticCalculator.div(6, 0);
    //		System.out.println("result:" + result);
    	}
    }
    
    测试结果:
    com.sun.proxy.$Proxy2
    -->validate:[3, 6]
    The method add begins...[3, 6]
    The method add ends...
    The method add ends with 9
    result:9
    -->validate:[6, 3]
    The method div begins...[6, 3]
    The method div ends...
    The method div ends with 2
    result:2
    

猜你喜欢

转载自blog.csdn.net/u011171125/article/details/85922579