AOP

详解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();
    }
}

public class ThrAdvice implements ThrowsAdvice {

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

 

 

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>

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值