实验五:SpringAOP案例实现

该博客介绍了如何在Spring框架中使用AOP进行切面通知的实现,分别通过XML配置和注解两种方式展示了后置通知、最终通知和异常通知的设置。在XML配置中,详细给出了配置切入点和切面的步骤,而在注解方式下,展示了如何使用@Aspect、@Pointcut等注解进行相同功能的实现。两种方法均在测试类中得到了验证。
摘要由CSDN通过智能技术生成

题目要求

有VisitService接口和它的实现类,作为切入类

public interface VisitService {
    void visit(String str) throws Exception;
}
public class VisitServiceImpl implements VisitService {
    @Override
    public void visit(String str) throws Exception {
        System.out.println(str);
        if (!str.equalsIgnoreCase("agree")) {
            throw new Exception("非法访问");
        }
    }
}

现有如下测试代码


public class SpringAOPTest {

    private VisitService visitService;
    @Before
    public void init() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        visitService = context.getBean(VisitService.class);

    }

    @Test
    public void testVisit() {
        try {
            visitService.visit("agree");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            visitService.visit("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

要求:利用SpringAOP实现:

  1. 最终通知打印输出“输入成功”
  2. 后置通知打印输出“请求成功,欢迎”
  3. 异常通知打印输出“请求失败,拒绝”

实验解答一 xml配置方式

根据题目中已有的代码,不难知道,题目考察AOP的切点与切面的织入操作。现要完成的操作是:新建一个切面类,并完成切面类与切点类的织入

下面我们按步骤来完成实验内容。

1. 在pom.xml中导入完成AOP的依赖
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
		<!--spring官方推荐用的aop依赖包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
2. 创建一个切面类VisitAspect,在类中按题目定义after、afterRunning、afterThrowing方法
public class VisitAspect {


    public void after() {
        System.out.println("输入成功");
    }

    public void afterRunning() {
        System.out.println("请求成功,欢迎");
    }

    public void afterThrowing() {
        System.out.println("请求失败,拒绝");
    }
}
3. 在Spring配置文件中完成切入点与切面的织入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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 id="visitAspect" class="com.yjx.service.impl.VisitServiceImpl"></bean>
    <bean id="aspect" class="com.yjx.aspect.VisitAspect"></bean>
    <aop:config>
    	<!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* *.*.service.impl.VisitServiceImpl.visit(*))"/>
        <!--切面-->
        <aop:aspect ref="aspect">
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterRunning" pointcut-ref="pointcut"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
    
</beans>
4. 测试结果

在这里插入图片描述

实验解法二 注解方式

注解方式与xml方式原理是属于一样的,只是 切入点与切面的织入是在切面类中完成,而xml中主要是完成注解的扫包操作。

1. VisitAspect类有很大的不同

@Component
@Aspect
public class VisitAspect {

    @Pointcut("execution(* *.*.service.impl.VisitServiceImpl.visit(*))")
    public void pt() {}

    @After("pt()")
    public void after() {
        System.out.println("输入成功");
    }
    @AfterReturning("pt()")
    public void afterRunning() {
        System.out.println("请求成功,欢迎");
    }
    @AfterThrowing("pt()")
    public void afterThrowing() {
        System.out.println("请求失败,拒绝");
    }
}
2. 而配置类中只有简短的两行扫包配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd
">
    <context:component-scan base-package="com.yjx"/>
    <aop:aspectj-autoproxy/>

</beans>
3. 测试结果与xml中的一致

在这里插入图片描述

源码下载

需要参考源码的同学们可以按照需求点击下载,这里提供xml、注解两个版本的源码下载,资源是免积分的,用于学习交流使用,请勿用于其他途径。
实验五:SpringAOP注解开发方式完成的小实验
实验五:SpringAOPXML配置文件完成的小实验

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值