spring AOP之基于xml配置文件的方式来配置AOP

前端之家收集整理的这篇文章主要介绍了spring AOP之基于xml配置文件的方式来配置AOP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Calculator.java

package com.gong.spring.aop.impl;

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

CalculatorImpl.java

 com.gong.spring.aop.impl2;

class CalculatorImpl implements Calculator{

    @Override
     j) {
        // TODO Auto-generated method stub
        int result =  i+j;
        return result;
    }

    @Override
    int result = i - j;
        int result = i *int result = i / result;
    }

}

LoggingAspect.java

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

 javax.management.RuntimeErrorException;

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


class LoggingAspect {
    
    void beforeMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println(methodName+" begin with "+args);
    }
    
     afterMethod(JoinPoint joinPoint) {
        获取名字
        String methodName = joinPoint.getSignature().getName();
        获取参数
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println(methodName+" end with "+args);
    }

     afterReturning(JoinPoint joinPoint,Object result) {
        String methodName = Arrays.asList(joinPoint.getArgs());
        System.out.println("在afterReturning得到返回值:"+ result);
        System.out.println(methodName+" end with "+ afterThrowing(JoinPoint joinPoint,Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+" occurs exception:"+ex);
    }
    
    public Object aroundMethod(ProceedingJoinPoint pjd) {
        Object result = null;
        String methodName = pjd.getSignature().getName();
        执行目标方法
        try {
            前置通知
            System.out.println(methodName+" begin with "+Arrays.asList(pjd.getArgs()));
            执行目标方法
            result = pjd.proceed();
            后置通知
            System.out.println("在aroundMethod中得到值:"+result);
        } catch (Throwable e) {
             TODO Auto-generated catch block
            异常通知
            System.out.println("the method occurs exception:" + e);
            throw new RuntimeException(e);
        }
        后置通知
        System.out.println(methodName+" end with "+Arrays.asList(pjd.getArgs()));
         result;
    }
}

applicationContext2.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.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    
    <!-- 配置bean -->
    bean id="calculator" class="com.gong.spring.aop.impl2.CalculatorImpl"></bean>
     配置切面bean ="loggingAspect"="com.gong.spring.aop.impl2.LoggingAspect" 配置AOP aop:config>
         配置切点表达式 -->
        aop:pointcut expression="execution(* com.gong.spring.aop.impl2.Calculator.*(int,int))" 
        id="pointcut"/>
         配置切面及通知 aop:aspect ref order="2">
            aop:before method="beforeMethod" pointcut-ref/>
            aop:after ="afterMethod"</aop:aspect>
beans>

需要注意的是:execution(* com.gong.spring.aop.impl2.Calculator.*(int,int))中额第一个*不要漏掉,否则会报错。

Main.java

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

 Main {
    static  main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext2.xml");
        从IOC容器中获取bean的实例
        Calculator calculator = (Calculator) ctx.getBean(Calculator.int res = calculator.add(2,1);
        System.out.println("在主函数中加法计算的结果="+res);
        res = calculator.div(2,1)">);
        System.out.println("在主函数中除法计算的结果="+res);
    }
}

输出

猜你在找的Spring相关文章