spring 中的aop配置

本文详细介绍了Spring中AOP的配置,包括依赖包的引入,四种切入通知(before、after-returning、after-throwing、after)的用法,特别指出在一次执行中最多执行三个通知,以及环绕通知的概念和实现,最后提到了AOP的注解方式配置。
摘要由CSDN通过智能技术生成

(1)依赖包:

 <dependencies>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.2.0.RELEASE</version>
       </dependency>

       <dependency>
           <groupId>org.aspectj</groupId>
           <artifactId>aspectjweaver</artifactId>
           <version>1.8.7</version>
       </dependency>
   </dependencies>

(2)bean配置:4种切入通知:

before:在切入方法之前执行;
after-returning :在切入方法正常执行结束之后执行;
after-throwing:在切入方法执行出现异常时执行;
after :不管切入方法正常执行或者异常执行,都会执行;
注:4种通知,一次执行顶多执行其中三个,before和after都会执行,after-returning和after-throwing依据否切入方法执行是否有异常

<?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"
       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
">
    <bean id="accountService" class="service.impl.AccountServiceImpl"></bean>

    <bean id="logger" class="utils.Logger"></bean>

    <aop:config>
        <aop:pointcut id="log" expression="execution(* service.impl.*..*(..))"></aop:pointcut>
        <aop:aspect id="logAdvice" ref="logger">
            <aop:before method="pringLogBefore" pointcut-ref="log"></aop:before>
            <aop:after-returning method="pringLogAfterReturn" pointcut-ref="log"></aop:after-returning>
            <aop:after-throwing method="pringLogAfterThrow" pointcut-ref="log"></aop:after-throwing>
            <aop:after method="pringLogAfter" pointcut-ref="log"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

(3)环绕通知:

环绕通知是spring支持通过代码方式实现更加灵活的方式进行对切入方法进行aop控制

<?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"
       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
">
    <bean id="accountService" class="service.impl.AccountServiceImpl"></bean>

    <bean id="logger" class="utils.Logger"></bean>

    <aop:config>
        <aop:pointcut id="log" expression="execution(* service.impl.*..*(..))"></aop:pointcut>
        <aop:aspect id="logAdvice" ref="logger">
            <!--<aop:before method="pringLogBefore" pointcut-ref="log"></aop:before>-->
            <!--<aop:after-returning method="pringLogAfterReturn" pointcut-ref="log"></aop:after-returning>-->
            <!--<aop:after-throwing method="pringLogAfterThrow" pointcut-ref="log"></aop:after-throwing>-->
            <!--<aop:after method="pringLogAfter" pointcut-ref="log"></aop:after>-->
            <aop:around method="printAround" pointcut-ref="log"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

通知方法:printAround

 public Object printAround(ProceedingJoinPoint pjt){
        Object ob = null;
        try{
            System.out.println("before");
             Object[] args = pjt.getArgs();
             ob = pjt.proceed(args);
            System.out.println("afterReturn");
             return ob;
        }catch(Throwable t){
            System.out.println("afterThrow");
        }
        finally {
            System.out.println("after");
        }
       return ob;
    }

(4)aop注解方式配置

(1)配置:

<?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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-context.xsd
">
    <!--配置spring容器扫描包-->
    <context:component-scan base-package="service,utils"></context:component-scan>
    <!--开启aop配置-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

(2)配置切面类:

package utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@Component("logger")
@Aspect  //表示当前类为切面类
public class Logger {
    
    // 配置切点表达式
    @Pointcut("execution(* service.*..*(..))")
    private void point1(){
        
    }
    @Before("point1()") //前置通知
    public void pringLogBefore(){
        System.out.println("before");
    }

    @AfterReturning("point1()") //后置通知
    public void pringLogAfterReturn(){
        System.out.println(" public void pringLogAfterReturn()");
    }

    @AfterThrowing("point1()") //异常通知
    public void pringLogAfterThrow(){
        System.out.println("  public void pringLogAfterThrow()");
    }
    @After("point1()")  //最终通知
    public void pringLogAfter(){
        System.out.println("  pringLogAfter");
    }

    @Around("point1()")  // 环绕通知
    public Object printAround(ProceedingJoinPoint pjt){
        Object ob = null;
        try{
            System.out.println("before");
             Object[] args = pjt.getArgs();
             ob = pjt.proceed(args);
            System.out.println("afterReturn");
             return ob;
        }catch(Throwable t){
            System.out.println("afterThrow");
        }
        finally {
            System.out.println("after");
        }
       return ob;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值