SpringAOP01-SpringAPI接口实现

本文详细介绍了如何在SpringAOP中利用SpringAPI接口实现切面编程。首先,创建Maven项目并导入Spring、JUnit等核心依赖。接着,定义接口及其实现类作为切入点,接着编写实现Spring接口的环绕类。然后,配置Spring的ApplicationContext.xml,将相关bean注册到IOC容器,并设置AOP的execution表达式。最后,通过测试验证AOP的正确执行。
摘要由CSDN通过智能技术生成

SpringAOP之SpringAPI接口实现

第一步创建maven项目导入依赖

	    <dependencies>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>

很简单的三个依赖,Spring依赖导入spring-webmvc后很多spring的包都被导入了,junit依赖单元测试,spring-webmvc植入包,用AOP必须导入这个依赖.

第二步

  1. 写一个接口和实现该接口的类,这个类就是我们的切入点,也就是我们要AOP的类.在实际开发中也就是我们的业务层.
    接口:
public interface Service {
    public void add();
    public void delete();
    public void update();
    public void query();
}

实现类:

public class ServiceImpl implements Service{
    public void add() {System.out.println("增加方法!");}

    public void delete() {System.out.println("删除方法!"); }

    public void update() {System.out.println("修改方法!"); }

    public void query() { System.out.println("查询方法!"); }
}
  1. 写我们要环绕的类,就是我们切入之后要执行的方法,类似于一个代理类,要实现Spring的接口.

方法执行之前: MethodBeforeAdvice

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("log"+o.getClass().getName()+"的"+method.getName()+"方法执行之前!");
    }
}

方法执行之后:

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("log"+method.getName()+"方法执行之hou!");
    }
}

以这两个为例子,点进springframework.aop中可以看到很多方法都可以配置.
在这里插入图片描述

第三步配置Spring配置文件AppliactionContext.xml

  1. 注册bean把三个类都放到IOC容器中
  2. 配置AOP:execution表达式是表示要找到的类的所有方法,括号中的…表示匹配所有参数类型的方法.
<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册Bean-->
    <bean id="ServiceImpl" class="com.shao.service.ServiceImpl"/>
    <bean id="Log" class="com.shao.log.Log"/>
    <bean id="AfterLog" class="com.shao.log.AfterLog"/>
    <!--配置AOP-->
    <aop:config>
    <!--切入点-->
    <!--expression:  表达式:execution(要执行的位置 *****)-->
        <aop:pointcut id="pointcut" expression="execution(* com.shao.service.ServiceImpl.*(..))"/>
    <!--执行环绕增加-->
        <aop:advisor advice-ref="Log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

第四步测试

public class MyTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("AppliactionContext.xml");
        Service serviceImpl = context.getBean("ServiceImpl", Service.class);
        serviceImpl.add();
    }
}

可以看到:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值