Spring源码解析系列之AOP(二)

本文深入解析Spring AOP的配置和源码追踪,通过赵本山和宋丹丹的小品例子,阐述面向切面编程的概念。文章详细介绍了配置面向切面编程的步骤,包括XML配置、bean的创建,以及源码跟踪分析,讲解了如何从配置到生成代理类的过程,帮助读者理解Spring AOP的内部工作原理。
摘要由CSDN通过智能技术生成

前言

在开始分析AOP之前,大家可以参考我的前2篇博客。
Java动态代理(https://www.jianshu.com/p/e6914f48f2a9)
Spring IOC(https://www.jianshu.com/p/3c70f548481d)
AOP即面向切面编程,其本质上就是把大多数接口都要做或者必须做的事情委托给代理类来做。比如所有人都要买房子,那么大家可以把这件事交给房产交易平台。网上博客翻了个底朝天也没有找到哪个博客把AOP源码解析部分讲的比较透彻的,大多数都让人摸不着头脑,即便给了aop入口说的也不是很清晰。本篇博客将以赵本山和宋丹丹的经典小品钟点工为例,讲讲把大象装冰箱的故事,来详细的描述下面向切面编程。
所用代码地址:https://github.com/KouLouYiMaSi/SpringDecode

一、Spring配置面向切面编程

1、ApplicationContext.xml配置

除了两个bean:zhaobenshan和adviceElephantTools,其他bean可以忽略。Aop配置中配置了切面myaop(切面想象成大象装冰箱服务公司),待切入的切入点logPointcut(这里使用了aspectj表达式,看看就行),切入的两个方法before和after。
值得注意的是本xml头部信息里需要有aop的相关声明!

<?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-4.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    <bean id="springHelloWorld"
        class="com.yiibai.tutorial.spring.helloworld.impl.SpringHelloWorld"></bean>
    <bean id="strutsHelloWorld"
        class="com.yiibai.tutorial.spring.helloworld.impl.StrutsHelloWorld"></bean>
    <bean id="helloWorldService" class="com.yiibai.tutorial.spring.helloworld.HelloWorldService">
        <property name="helloWorld" ref="strutsHelloWorld" />
    </bean>
    <bean id="zhaobenshan" class="com.yiibai.tutorial.spring.aop.Zhaobenshan" />
    <bean id="adviceElephantTools" class="com.yiibai.tutorial.spring.aop.AdviceElephantTools" />
    <aop:config>
        <aop:aspect id="myaop" ref="adviceElephantTools">  <!--切面名称-->
            <aop:pointcut id="logPointcut"
                expression="execution(* com.yiibai.tutorial.spring.aop.*.*(..))" /> <!--切入点-->
            <aop:before method="before" pointcut-ref="logPointcut" /> <!--前置方法-->
            <aop:after method="after" pointcut-ref="logPointcut" /> <!--后置方法-->
        </aop:aspect>
    </aop:config>
</beans>

2、两个bean配置

这里为了使用JDK动态代理而设计了一个接口,spring aop会默认使用jdk动态代理,在没有接口的情况下会使用cglib动态代理。
赵本山需要实现大象工具接口来把大象装冰箱里,但是大象工具切面类还提供了打开冰箱门和关闭冰箱门等操作。如果世界上有1000个赵本山那么只需要一个大象切面类就能为所有赵本山提供打开冰箱门和关闭冰箱门操作。

public interface PuttingTool {
    public abstract void putElephantToRefrigerator();
}
public class Zhaobenshan implements PuttingTool {

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值