spring-aop 基本使用

spring-aop 基本使用

spring 的注解方式的 aop 步骤:
关键三个步骤如下:
1)、将业务逻辑组件和切面类都加在容器中,告诉spring哪个是切面类(@Aspect)
2)、在切面类上的每个通知方法上标注通知注解,告诉spring何时何地运行(切入点表达式)
3)、开启基于注解的aop模式,@EnableAspectJAutoProxy

具体步骤如下:

1,导入aop模块:spring aop:(spring-aspects)
2,定义一个业务逻辑类(MathCalculator);业务逻辑运行的时候讲日志进行打印(方法之前,方法运行结束之后)
3,定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里
     通知方法:
          前置通知(@Before):logStart:在目标方法(div)运行之前运行
         后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
         返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
          异常通知(@AfterThrowing):logException:目标方法(div)出现异常以后运行
          环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
 4,给切面类的目标方法标注何时何地运行(通知注解)
5,将切面类和业务逻辑类(目标方法所在类)都加入到容器中
 6,必须告诉spring哪个类是切面类(给切面类上加一个注解,@Aspect)
 [7],给配置类中加@EnableAspectJAutoProxy【开启基于注解的aop模式】

1,导入maven依赖

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>

2,编写切面类

package com.test.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 切面类
 */
@Aspect
//@Component
public class LogAspect {

    // 抽取公共的切入点表达式
    // 1,本类引用
    // 2,其他的切面引用; com.test.spring.aop.LogAspect.pointCut()
    @Pointcut("execution(public * com.test.spring.aop.MathCalculator.*(..))")
    public void pointCut(){

    }

    // @Before 在目标方法之前切入,切入点表达式(指定在哪个方法切入)
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(joinPoint.getSignature().getName() + "方法运行。。。,参数列表是:{"+ Arrays.asList(args)+"}");
    }

    @After("com.test.spring.aop.LogAspect.pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName() + "方法运行结束。。。");
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result){
        System.out.println(joinPoint.getSignature().getName() + "方法正常执行的返回结果。。。,结果为:{"+result+"}");
    }

    @AfterThrowing(value = "pointCut()", throwing = "exception")
    public void logException(JoinPoint joinPoint, Exception exception){
        exception.printStackTrace();
        System.out.println(joinPoint.getSignature().getName() + "方法执行发生异常,异常信息:{}");
    }

}

3,配置类

package com.test.spring.aop;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 1,导入aop模块:spring aop:(spring-aspects)
 * 2,定义一个业务逻辑类(MathCalculator);业务逻辑运行的时候讲日志进行打印(方法之前,方法运行结束之后)
 * 3,定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里
 *      通知方法:
 *          前置通知(@Before):logStart:在目标方法(div)运行之前运行
 *          后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
 *          返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
 *          异常通知(@AfterThrowing):logException:目标方法(div)出现异常以后运行
 *          环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
 * 4,给切面类的目标方法标注何时何地运行(通知注解)
 * 5,将切面类和业务逻辑类(目标方法所在类)都加入到容器中
 * 6,必须告诉spring哪个类是切面类(给切面类上加一个注解,@Aspect)
 * [7],给配置类中加@EnableAspectJAutoProxy【开启基于注解的aop模式】
 *
 */
@Configuration
@EnableAspectJAutoProxy
public class MainConfigOfAop {

    @Bean
    public MathCalculator calulate(){
        return new MathCalculator();
    }

    @Bean
    public LogAspect logAspect(){
        return new LogAspect();
    }
}

4,业务类

package com.test.spring.aop;

/**
 *
 */
public class MathCalculator {

    public int div(int i, int j){
        System.out.println("...MathCalculator.div..方法运行。。。。");
        return i/j;
    }

}

5,测试类

package com.test.spring;

import com.test.spring.aop.MainConfigOfAop;
import com.test.spring.aop.MathCalculator;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 
 */
public class IocTest_Aop {

    @Test
    public void test(){
        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext(MainConfigOfAop.class);

        // 不能直接new对象,需要在spring容器中获取对象才能使用触发aop的作用
//        MathCalculator mathCalculator = new MathCalculator();
//        mathCalculator.div(1,1);

        MathCalculator calculator = context.getBean(MathCalculator.class);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值