AOP实现的三种方法 切入点 切入面

1.AOP实现的原生方法spring的API接口

动态代理类似

SpringAPI接口实现类

public class BeforeLog implements MethodBeforeAdvice {
    /**
     *
     * @param method 方法
     * @param args 参数
     * @param target 目标对象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+"方法");
    }
}

spring配置文件的编写

<?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到spring容器中-->
    <bean id="UserImpl" class="service.UserImpl"/>
    <bean id="BeforeLog" class="log.BeforeLog"/>

    <aop:config>
<!--    添加切入点  execution(* service.UserImpl.*(..))表示切入到这个类的所有方法-->
        <aop:pointcut id="pointcut" expression="execution(* service.UserImpl.*(..))"/>
<!--    设置切入的日志类  pointcut-ref切入到哪里 -->
        <aop:advisor advice-ref="BeforeLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

接口和接口实现类的编写

package service;

/**
 * @author:LeeGaki
 * @date:2022/1/13
 */
public class UserImpl implements User{
    @Override
    public void add() {
        System.out.println("add");
    }

    @Override
    public void sel() {
        System.out.println("sel");

    }

    @Override
    public void del() {
        System.out.println("del");

    }

    @Override
    public void upd() {
        System.out.println("upd");

    }
}

测试类的编写

    @Test
    public void log(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取的返回值要是接口,获取的bean应该是实现类
        User userImpl = context.getBean("UserImpl", User.class);
        userImpl.add();
    }

2.AOP实现的第二种方法

自定义类(切面)的实现

配置文件中aop的配置

    <aop:config>
<!--        创建切入点 第一个*代表返回值类型 后面表示切入到这个类的所有方法-->
        <aop:pointcut id="point" expression="execution(* service.UserImpl.*(..))"/>
<!--        ref后是自定义切面 也就是自定义的类-->
        <aop:aspect ref="DiyLog">
<!--            切入到哪个方法的前还是后-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

自定义类(切面)

package log;

/**
 * @author:LeeGaki
 * @date:2022/1/13
 */
public class DiyLog {
    public void before(){
        System.out.println("====执行前========");
    }
    public void after(){
        System.out.println("====执行后========");
    }
}

测试类

    @Test
    public void log(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取的返回值要是接口,获取的bean应该是实现类
        User userImpl = context.getBean("UserImpl", User.class);
        userImpl.add();
    }

3.注解式实现aop

首先打开aop注解支持在配置文件中加入

<aop:aspectj-autoproxy/>

编写自定义类 添加注解

package log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @author:LeeGaki
 * @date:2022/1/13
 */
@Aspect
public class DiyLog {
    @Before("execution(* service.UserImpl.*(..))")
    public void before(){
        System.out.println("====执行前========");
    }
    @After("execution(* service.UserImpl.*(..))")
    public void after(){
        System.out.println("====执行后========");
    }

    @Around("execution(* service.UserImpl.*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("环绕前");
        Object proceed = pj.proceed();
        System.out.println("环绕后");
    }
}

前后环绕都存在的时候实现的顺序是

环绕前
====执行前========
add
====执行后========
环绕后
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LeeGaKi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值