zoukankan      html  css  js  c++  java
  • Spring学习AOP(二)

    接口

    package com.spring.aop;
    
    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);
    	
    }  

     实现类

    package com.spring.aop;
    
    import org.springframework.stereotype.Component;
    
    @Component("arithmeticCalculator")
    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;
    	}
    
    }
    

      

    AOP类

    LoggingAspect

    package com.spring.aop;
    
    import java.util.ArrayList;
    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;
    
    /*
     * @Component 被Spring管理
     * @Aspect	声明为切面
     * @Order 切面优先级,值越小优先级越高
     */
    @Order(1)
    @Component
    @Aspect
    public class LoggingAspect {
    	
    	/**
    	 * 定义一个方法,用于声明切入点表达式。
    	 * 使用@Pointcut来声明切入点表达式
    	 * 后面的其他通知直接通过方法名引用当前表达式
    	 */
    	@Pointcut("execution(public * ArithmeticCalculator.*(..))")
    	public void declareJoinPointExpression(){}
    	
    
    //	/**
    //	 * 前置通知
    //	 * 在ArithmeticCalculator接口的每一个实现类方法执行之前执行
    //	 * @param joinPoint
    //	 */
    //	@Before("execution(public * ArithmeticCalculator.*(..))")
    //	public void beforeMethod(JoinPoint joinPoint){
    //		String methodName = joinPoint.getSignature().getName();
    //		Object[] args =joinPoint.getArgs();
    //		System.out.println("the method"+methodName+" start with"+Arrays.asList(args));
    //		
    //	}
    //	/**
    //	 * 后置通知
    //	 * 方法执行完之后执行的代码,无论是否出现异常都执行
    //	 * @param joinPoint
    //	 */
    //	@After("execution(public * ArithmeticCalculator.*(..))")
    //	public void afterMethod(JoinPoint joinPoint){
    //		String methodName = joinPoint.getSignature().getName();
    //		System.out.println("the Method "+methodName+" ends");
    //	}
    //	
    //	
    //	/**
    //	 * 返回通知
    //	 * 在方法正常结束之后执行的通知 
    //	 * 可以访问到方法的返回值
    //	 * @param joinPoint
    //	 * @param result
    //	 */
    //	@AfterReturning(value="execution(public * ArithmeticCalculator.*(..))",returning="result")
    //	public void afterReturningMethod(JoinPoint joinPoint,Object result){
    //		String methodName = joinPoint.getSignature().getName();
    //		System.out.println("the method"+methodName+" end with "+result);
    //		
    //	}
    //	
    //	/**
    //	 * 异常通知
    //	 * 在方法执行发生异常时执行的方法
    //	 * 可以返回异常
    //	 * @param joinPoint
    //	 * @param exception
    //	 */
    //	@AfterThrowing(value="execution(public * ArithmeticCalculator.*(..))",throwing="exception")
    //	public void afterThrowingMethod(JoinPoint joinPoint,Exception exception){
    //		String methodName = joinPoint.getSignature().getName();
    //		System.out.println("the method "+methodName+"occus exception "+exception);
    //		
    //	}
    	
    	/**
    	 * 环绕通知
    	 */
    	@Around("declareJoinPointExpression()")
    	public Object aroundMethod(ProceedingJoinPoint joinPoint){
    		Object result = null;
    		String methodName = joinPoint.getSignature().getName();
    		
    		try {
    //			前置通知
    			System.out.println("the method "+methodName+"start with "+Arrays.asList(joinPoint.getArgs()));
    //			执行方法
    			result = joinPoint.proceed();
    //			返回通知
    			System.out.println("the method "+methodName+"end 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;
    	}
    	
    	
    }
    

      

    Spring配置文件

    applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     9     
    10     <!-- 配置自动扫描的包 -->    
    11     <context:component-scan base-package="com.spring.aop"></context:component-scan>      
    12     
    13     <!-- 使 aspect 注解起作用 -->
    14     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>    
    15      
    16 </beans>

    所需Jar包

    com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    commons-logging-1.1.1.jar
    spring-aop-4.0.0.RELEASE.jar
    spring-aspects-4.0.0.RELEASE.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar

  • 相关阅读:
    python入门第十七天_生成器 迭代器
    python入门第十六天__列表生成式
    装饰器补充知识点_ @functools.wraps(func)
    函数练习题2
    函数编程练习题1
    迭代器
    生成器的send方法
    函数写生成器
    斐波那契数列
    生成器
  • 原文地址:https://www.cnblogs.com/duyunchao-2261/p/7490951.html
Copyright © 2011-2022 走看看