欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AOP

程序员文章站 2022-07-12 14:17:21
...

详解AOP机制:

https://baijiahao.baidu.com/s?id=1613310315603029991&wfr=spider&for=pc

AOP-schema base

public class User {
    void A(){
        System.out.println("方法A");
    }
    public void B(){
        System.out.println("方法B");
    }
    void C(){
        System.out.println("方法C");
    }
}
public class AfterMethodB implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("---后置通知---");
    }
}
public class BeforeMethodB implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("---前置通知---");
    }
}
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class RundMethodB implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        System.out.println("环绕通知--前");
        //执行切点中的目标方法
        Object proceed = methodInvocation.proceed();
        System.out.println("环绕通知--后");

        return proceed;
    }
}
<?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:tx="http://www.springframework.org/schema/tx"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="user" class="test.User"></bean>

    <!--配置前置通知对象-->
    <bean id="before" class="test.BeforeMethodB"></bean>
    <!--配置后置通知对象-->
    <bean id="after" class="test.AfterMethodB"></bean>
    <!--配置环绕通知对象-->
    <bean id="rund" class="test.RundMethodB"></bean>
    <!--配置切面-->
    <aop:config>
        <!--配置切点-->
        <aop:pointcut id="pt1" expression="execution(* test.User.B())"></aop:pointcut>
        <aop:advisor advice-ref="before" pointcut-ref="pt1"></aop:advisor>
        <aop:advisor advice-ref="after" pointcut-ref="pt1"></aop:advisor>
        <aop:advisor advice-ref="rund" pointcut-ref="pt1"></aop:advisor>
    </aop:config>

</beans>
public class Test {
    public static void main(String[] args) {

        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext001.xml");

        User user = app.getBean("user", User.class);

        user.B();
    }
}

AOP

AOP

public class ThrAdvice implements ThrowsAdvice {

    public void afterThrowing(Exception ex) throws Throwable{
        System.out.println("异常通知!!!");
    }
}

AOP

AOP

AOP

AOP

AOP

 

AOP

 

AOP-Aspect J

public class AspectJ {

    public void beforeMethodA(){
        System.out.println("--前置通知--");
    }

    public void afterMethodA(){
        System.out.println("--后置通知--");
    }

    public void roundMethodA(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知--前");
        Object proceed = joinPoint.proceed();
        System.out.println("环绕通知--后");
    }

    public void ThrowMethodA(){
        System.out.println("异常通知");
    }
}
<bean id="asp" class="test.AspectJ"></bean>
<aop:config>
    <aop:aspect ref="asp">
        <aop:pointcut id="pt2" expression="execution(* test.User.A())"></aop:pointcut>
        <aop:before method="beforeMethodA" pointcut-ref="pt2"></aop:before>
        <aop:after method="afterMethodA" pointcut-ref="pt2"></aop:after>
        <aop:around method="roundMethodA" pointcut-ref="pt2"></aop:around>
        <aop:after-throwing method="ThrowMethodA" pointcut-ref="pt2"></aop:after-throwing>
    </aop:aspect>
</aop:config>

AOP

AOP

 

AOP

AOP

 

 

 

相关标签: 基础知识的理解