Spring学习笔记【part08】AOP注解开发

导读:本篇文章讲解 Spring学习笔记【part08】AOP注解开发,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Spring 学习笔记 Part08

1. AOP注解开发前的配置

导入注解开发需要的context命名空间。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

配置IoC注入容器的注解,并在java包下配置@Service(“accountService”)、@Repository(“accountDao”)和@Component(“logger”)。

<context:component-scan base-package="com.itheima"/>

开启spring对AOP注解的支持

<aop:aspectj-autoproxy/>

2. 注解标签介绍

@Aspect:表明当前类是一个切面类

@Component("logger")
@Aspect
public class Logger {
    
}

@Before:表明当前方法是一个前置通知

@AfterReturning:表明当前方法是一个后置通知

@AfterThrowing:表明当前方法是一个异常通知

@After:表明当前方法是一个最终通知

以上四个的共有属性:

​ value:用于指定切入点表达式(execution关键字),或者指定切入点表达式的引用(配合@Pointcut注解)。

我们会在开发中发现@AfterReturning和@After两个通知的顺序可能会乱,因此在注解开发方式中,建议采用@Around方式。

@Component("logger")
@Aspect
public class Logger {
    
    /**
     * 前置通知
     */
    @Before("execution(* com.itheima.service.impl.*.*(..))")
    public  void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }

    /**
     * 后置通知
     */
    @AfterReturning("execution(* com.itheima.service.impl.*.*(..))")
    public  void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    
    /**
     * 异常通知
     */
    @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))")
    public  void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }

    /**
     * 最终通知
     */
    @After("execution(* com.itheima.service.impl.*.*(..))")
    public  void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
    
}

@Pointcut:指定切入点表达式

属性:

​ value:指定表达式的内容(execution关键字)

切入点表达式注需要建立一个函数,private void 任取函数名,函数体为空。任取的函数名为各类通知引用时使用的id。在各类通知的value属性中引用切入点表达式时,注意value里的切入点表达式id名后必须加括号(),否则将会编译错误。

@Component("logger")
@Aspect
public class Logger {
    
	@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    private void pt1(){}

    /**
     * 前置通知
     */
    @Before("pt1()")
    public  void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }

    /**
     * 后置通知
     */
    @AfterReturning("pt1()")
    public  void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    
    /**
     * 异常通知
     */
    @AfterThrowing("pt1()")
    public  void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }

    /**
     * 最终通知
     */
    @After("pt1()")
    public  void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
    
}

@Around:表明当前方法是一个环绕通知

属性:

​ value:用于指定切入点表达式,还可以指定切入点表达式的引用。

@Component("logger")
@Aspect
public class Logger {
    
	@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    private void pt1(){}

    @Around("pt1()")
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法执行所需的参数

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");

            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");

            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
    
}

3. 进阶注解(彻底不使用 XML)

@EnableAspectJAutoProxy :开启spring对AOP注解的支持

在本part第1节中的扫描包和AOP注解开启都是用XML配置的

<context:component-scan base-package="com.itheima"/>
<aop:aspectj-autoproxy/>

我们可以在自定义的SpringConfiguration类中用注解配置,彻底和XML说拜拜

@Configuration
@ComponentScan(basePackages="com.itheima")
@EnableAspectJAutoProxy
public class SpringConfiguration {
    	
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/84497.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!