spring-aop源码解析之环境搭建

本文通过一个简单的例子介绍了如何在Spring中使用全注解方式实现AOP,包括配置AopConfig、定义LogPrintAspect切面类、设置切点和通知方法,以及在AopService中调用方法触发AOP。展示了AOP在日志记录、事务管理等场景的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  1. 在前面讲过了spring的ioc容器的创建以及bean的实例化过程,后面继续介绍了springmvc的执行流程。至此,我们在web开发中最核心的两个模块的大致运行流程我们已经有了一个模糊的印象了

  2. 但是,web开发中不仅仅只是ioc对于bean的管理以及我们的servlet的使用。在日常开发中,我们也需要做一些简单的日志,或者在crud中需要对数据库的事务进行管理,还有对于一些业务需要进行缓存也可以使用aop去统一处理,权限认证呀(一般是在filter或者interceptor中去做的),数据库资源切换等等,都可以使用aop去做。aop可以将我们与核心业务无关的一些代码分离开,统一去包装,来重复去使用。

关于aop的介绍去百度吧。我也不多哔哔了,直接开始我的表演🎭🎭🎭🎭🎭🎭🎭

首先开始搭建一个最简单的aop功能。 这里我们就是用全注解的方式了。

MainConfig

/**
 * @author 蒙ym
 * @Date 2021/1/20
 */
@ComponentScans({@ComponentScan("com.aop.*"),@ComponentScan("com.aop")})
//开启aop功能!!!这个注解很重要!!!核心注解!!!
@EnableAspectJAutoProxy
public class AopConfig {
}

mainConfig非常简单,配置包扫描的路径,开启aop功能就可以了。
由于我们指定就是加载这个AopConfig,所以@Configuration注解都不需要。

LogPrintAspect

/**
 * @author 蒙ym
 * @Date 2020/12/11
 */
@Aspect
@Component
public class LogPrintAspect {

    @Pointcut("execution(* com.aop.AopService.aopService(..))")
    public void method(){};

    @Before("method()")
    public void before(JoinPoint joinPoint){
        System.err.println("before~~~~~~");
        System.err.println(joinPoint.toString());
        System.err.println("before~~~~~~");
    }

    @After("method()")
    public void afrer(JoinPoint joinPoint){
        System.err.println("after~~~~~~");
        System.err.println(joinPoint.toString());
        System.err.println("after~~~~~~");
    }

    @Around("method()")
    public Object arround(ProceedingJoinPoint pjp) {
        System.out.println("方法环绕start.....");
        try {
            Object o =  pjp.proceed();
            System.err.println("方法环绕proceed,结果是 :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}

这里有两个核心 @Aspect标注他是一个切面类 @Component将他加入到ioc容器中
@Pointcut里面设置切点,也就是我们需要在哪一个方法执行前后,先来去执行aop中的通用处理
@Before @After @Around也就是在执行方法的前后等等先来执行这个注解标注的方法。

上面这一段看不懂没关系,分析源码过后就知道他是干嘛的了,不用去咬字眼,说一个一个的名词的意思。

AopService

@Service("aopService")
public class AopService {

    public String aopService(String name, List list, Map map, Student student){
      System.err.println("测试aop的service......");
      return "success";
    }
}

主测试方法

/**
 * @author 蒙ym
 * @Date 2021/1/20
 */
public class AopMainApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AopConfig.class);
        AopService aopService = applicationContext.getBean("aopService", AopService.class);
        String name = "我是测试名字";
        List<String> list = new ArrayList<>();
        list.add("我是测试的list");
        Map<String,List<String>> map = new HashMap<>();
        map.put(name, list);
        Student student = new Student();
        student.setName("我是测试的学生的名字");
        aopService.aopService(name, list, map, student);

    }
}

直接看结果
在这里插入图片描述

注意,我的配置类没有加@Configuration。 所以@Configuration与@Component之间的区别到底是怎么样的?

从我们前面的分析来看,在spring中,他会将@Component标注的类也当成配置类。所以具体的区别以后有时间了在深入分析吧。 就我们目前分析的逻辑中,@Component完全可以取代@Configuration

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值