基于注解的 AOP 开发教程

这篇博客详细介绍了基于注解的AOP开发过程,包括创建目标接口和类、定义切面类、将对象托管给Spring、配置注解织入、开启组件扫描和AOP代理,以及测试环节。尽管减少了XML配置,但仍然需要一些XML来启动组件扫描和AOP功能。
摘要由CSDN通过智能技术生成

快速入门

一、基于注解的aop开发步骤:

① 创建目标接口和目标类(内部有切点)

② 创建切面类(内部有增强方法)

③ 将目标类和切面类的对象创建权交给 spring

④ 在切面类中使用注解配置织入关系

我们还抽取了切点表达式:
在这里插入图片描述在这里插入图片描述1、2、3、4的代码:
TargetInterface :

package cn.itcast.aop_anno;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/12 19:56
 */
public interface TargetInterface {
    public void save();
}

Target :

package cn.itcast.aop_anno;

import org.springframework.stereotype.Component;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/12 19:57
 */

@Component("target")
public class Target implements TargetInterface {
    public void save() {

        System.out.println("aop_anno run...");
        //int a=1/0;
    }
}

MyAspect :

package cn.itcast.aop_anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/13 9:39
 */

@Component("myAspect")
@Aspect  //必须加,不然只有你知道这是个aspect,spring不认识的
public class MyAspect {
    //@Before("execution(* cn.itcast.aop_anno.*.*(..))")
    public void before(){
        System.out.println("前置增强...");
    }

    //@AfterReturning("execution(* cn.itcast.aop_anno.*.*(..))")
    public void afterReturning1(){
        System.out.println("后置增强...");
    }

    //Proceeding JoinPoint:  正在执行的连接点===切点
    //@Around("execution(* cn.itcast.aop_anno.*.*(..))")
    @Around("MyAspect.mypointcut()")
    public Object around1(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕增强前...");
        Object proceed = pjp.proceed(); //切点方法
        System.out.println("环绕后增强...");
        return proceed;
    }


    public void afterThrowing(){
        System.out.println("异常抛出增强..........");
    }

    //@After("execution(* cn.itcast.aop_anno.*.*(..))")
    public void after(){
        System.out.println("最终增强..........");
    }

    //抽取定义切点表达式
    @Pointcut("execution(* cn.itcast.aop_anno.*.*(..))")
    public void mypointcut(){}
}

⑤ 在配置文件中开启组件扫描和 AOP 的自动代理

4、5点我们新建一个applicationContext-aop_anno.xml,前面要加上context的网址…:

<?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: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/context  http://www.springframework.org/schema/context/spring-context.xsd">


    <!--组件扫描-->
    <context:component-scan base-package="cn.itcast.aop_anno"></context:component-scan>

    <!--aop自动代理,必须加-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

⑥ 测试

AopAnnoTest :

package com.itcast;

import cn.itcast.aop_anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/13 9:57
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-aop_anno.xml")
public class AopAnnoTest {
    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){

        target.save();

    }
}

测试around成功
在这里插入图片描述

二、小结

总的来说基于注解的 AOP 它还是需要xml文件的,但只不过它就把里面的一些内容绑定给一些切面类等…,它没有新注解一点都不需要xml的情况,但是我感觉他和基于 XML 的 AOP 开发入门相比,就是在xml少写一点东西,但还是需要xml来开启组件扫描和 AOP 的自动代理,感觉更麻烦了。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值