SpringAOP-xml配置AOP-xml+注解配置AOp

Posted linglongfang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringAOP-xml配置AOP-xml+注解配置AOp相关的知识,希望对你有一定的参考价值。

实体类如下

Calculation是我们用的类,PointCut是我们要实现通知的切面类

Calculation类如下

package com.llf.aop;

import org.springframework.stereotype.Component;

/**
 * @author linglongfang
 */
@Component
public class Calculation {

    public int add(int i,int j){
        System.out.println("执行add");
        return i+j;
    }

    public int sub(int i,int j){
        System.out.println("执行sub");
        return i-j;
    }

    public int mul(int i,int j){
        System.out.println("执行mul");
        return i*j;
    }

    public int div(int i,int j){
        System.out.println("执行div");
        return i/j;
    }
}

 

PointCut类如下

package com.llf.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author linglongfang
 */
@Order(1)
@Aspect
@Component
public class PointCut {
   //切点
    @Pointcut("execution(* com.llf.aop.Calculation.*(int, int))")
    public void defaultPointCut(){}


    //前置通知
    @Before("defaultPointCut()")
    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("执行方法前:"+methodName+"参数为"+args);
    }

    //后置通知  后置通知拿不到返回值
    @After("defaultPointCut()")
    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法后:"+methodName);
    }

    //返回通知
    @AfterReturning(value="defaultPointCut()",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法返回后:"+methodName+":结果是"+result);
    }

    //异常通知
    @AfterThrowing(value="defaultPointCut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法异常后:"+methodName+":抛出"+ex);
    }

      //环绕通知
//    @Around("defaultPointCut()")
//    public Object aroud(ProceedingJoinPoint pjd){
//        Object result = null;
//        try {
//            System.out.println("前置通知");
//            result = pjd.proceed();
//            System.out.println("返回通知");
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//            System.out.println("异常通知");
//        }
//        System.out.println("后置通知");
//        return result;
//    }

}

 

 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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--扫描包-->
    <context:component-scan base-package="com.llf.aop"></context:component-scan>
    <!--开启aop-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 </beans>

 

测试类Main:

package com.llf.aop;

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

/**
 * @author linglongfang
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new  ClassPathXmlApplicationContext("aop.xml");
        Calculation calculation = (Calculation) ctx.getBean("calculation");
        calculation.add(1, 3);
    }
}

 

测试结果:

 

以上是关于SpringAOP-xml配置AOP-xml+注解配置AOp的主要内容,如果未能解决你的问题,请参考以下文章

spring配置使用xml还是注解好

Spring使用@Scheduled注解配置定时任务

Spring Boot @Value注解中文配置乱码

Spring 注解开发 -- Spring新注解(完全替换Spring配置文件 使用配置类)

java注解开发是啥?是否不用在xml里配置ssh?以前都是配置xml,注解让我晕了

基于注解配置扫描包