spring aop

spring aop

package org.study.spring.aopanno;

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

@Component
@Aspect
@Order(1)//优先级 顺序从小类的开始执行
public class UserProxy {


    //前置通知,切入点表达式
    @Before(value = "execution(* org.study.spring.aopanno.User.add(..))")
    public void before() {
        System.out.println("before");
    }

    //最终通知
    @After(value = "execution(* org.study.spring.aopanno.User.add(..))")
    public void after() {
        System.out.println("after");
    }

    //异常通知
    @AfterThrowing(value = "execution(* org.study.spring.aopanno.User.add(..))")
    public void afterThrowing() {
        System.out.println("afterThrowing");
    }

    //后置通知
    //异常不执行
    @AfterReturning(value = "execution(* org.study.spring.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning");
    }

    //环绕,之前之后 执行
    @Around(value = "execution(* org.study.spring.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before");
        proceedingJoinPoint.proceed();
        //异常不执行
        System.out.println("around after");
    }

    //相同切入点抽取
    @Pointcut(value = "execution(* org.study.spring.aopanno.User.add(..))")
    public void pointcut(){
        System.out.println("pointcut");
    }

    @After(value = "pointcut()")
    public void afterPointcut(){
        System.out.println("afterPointcut");
    }
    @Before(value = "pointcut()")
    public void beforePointcut(){
        System.out.println("beforePointcut");
    }

}

package org.study.spring.config;

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

@Configuration
@ComponentScan(basePackages = {"org.study.spring.aopanno"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopConfig {
}

<?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 https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="book" class="org.study.spring.aopanno.Book"></bean>
    <bean id="bookProxy" class="org.study.spring.aopanno.BookProxy"></bean>

    <aop:config>
        <!--公共切入点配置 -->
        <aop:pointcut id="bookPointcut" expression="execution(* org.study.spring.aopanno.Book.add())" ></aop:pointcut>

        <!--pointcut-ref 公共切入点。pointcut切入点。-->
        <aop:aspect id="bookProxyAspect" ref="bookProxy" order="1">
            <aop:before method="before" pointcut-ref="bookPointcut"></aop:before>

            <aop:after-throwing method="afterThrowing" pointcut-ref="bookPointcut"></aop:after-throwing>
            <aop:after-returning method="afterReturning" pointcut="execution(* org.study.spring.aopanno.Book.add())"></aop:after-returning>
            <aop:after method="after" pointcut-ref="bookPointcut"></aop:after>

            <aop:around method="around" pointcut="execution(* org.study.spring.aopanno.Book.add())"></aop:around>

        </aop:aspect>

    </aop:config>
</beans>
执行顺序
around before
before
add
afterReturning
afterPointcut
after
around after

留下回复